动态规划之背包模型例题

1. HDU 1203 I NEED A OFFER!

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1203

稍微转化下,dp[i]表示的是前i个学校不被接受的最小概率。

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD =  10007;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int n,m;
double dp[maxn];
int v[maxn];
double g[maxn];
double ans=0;
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(n==0&&m==0) break;
        for (int i=1;i<=m;i++)
        {
            scanf("%d%lf",&v[i],&g[i]);
        }
        dp[0]=1;
        for (int i=0;i<=n;i++) dp[i]=1;
        for (int i=1;i<=m;i++)
        {
            for (int j=n;j>=v[i];j--)
            {
                dp[j]=min(dp[j],dp[j-v[i]]*(1-g[i]));
            }
        }
        double ans=0;
        for (int i=0;i<=n;i++)
        {
            ans=max(ans,(1-dp[i])*100);
        }
        printf("%.1f%%\n",ans);
    }
	return 0;
}

2.  HDU  2546 饭卡

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=2546

先将m减去5,选物品中的最大值,然后dp

代码如下:

//include <bits/stdc++.h>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <time.h>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD =  10007;
const int maxn = 1e3+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3f;
template <class T>
inline bool scan_d(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
int n,m;
int v[maxn];
int dp[maxn];
int main()
{
    while(scanf("%d",&n)&&n)
    {
        int Max=-1,loc=0;
        for (int i=1;i<=n;i++)
        {
            scanf("%d",&v[i]);
            if(Max<v[i])
            {
                Max=v[i];
                loc=i;
            }
        }
        scanf("%d",&m);
        m-=5;
        memset (dp,0,sizeof(dp));
        for (int i=1;i<=n;i++)
        {
            if(i==loc) continue;
            for (int j=m;j>=v[i];j--)
            {
                dp[j]=max(dp[j],dp[j-v[i]]+v[i]);
            }
        }
        int ans=0;
        for (int i=0;i<=m;i++)
        {
            dp[i]+=Max;
            ans=max(ans,dp[i]);
        }
        printf("%d\n",m+5-ans);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/89640402
今日推荐