Cow Exhibition [POJ2184] [DP] [背包的负数处理]

题意:

有很多羊,每只羊有一个幽默度和智商,要选出一些羊,智商加幽默度总和最大,其中智商总和和幽默度总和都不能是负数。

样例输入:

5

-5 7

8 -6

6 -3

2 1

-8 -5

样例输出:

8

分析:

这很像一个01背包题,但是有两个价值,却没有重量

我们就把其中一个看成重量,另一个看成价值

答案就是max(i+dp[i])

那如何保证数据大于0呢?

我们可以把价值总体向右移100000个单位,然后设原点O为100000

之后就可以进行dp了

转移方程仍是:dp[j]=dp[j-w[i]]+v[i];

但要注意,如果w[i]>=0要逆向循环,w[i]<0要顺向循环,确保是01背包而不是完全背包

最后计算结果时,从O向右枚举,保证了重量不为负数,dp[i]>=0,保证价值不为负数

再从中选max(i-O+dp[i])

代码:

 1 #include<set>
 2 #include<map>
 3 #include<queue>
 4 #include<stack>
 5 #include<cmath>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<iostream>
 9 #include<algorithm>
10 #define RG register int
11 #define rep(i,a,b)    for(RG i=a;i<=b;++i)
12 #define per(i,a,b)    for(RG i=a;i>=b;--i)
13 #define ll long long
14 #define inf (1<<29)
15 #define O 100000
16 #define maxn 105
17 #define maxm 200005
18 using namespace std;
19 int n;
20 int dp[maxm],v[maxn],w[maxn];
21 inline int read()
22 {
23     int x=0,f=1;char c=getchar();
24     while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
25     while(c>=‘0‘&&c<=‘9‘){x=x*10+c-‘0‘;c=getchar();}
26     return x*f;
27 }
28 int main()
29 {
30     n=read();
31     rep(i,1,n)    w[i]=read(),v[i]=read();
32     //memset(dp,-63,sizeof(dp));
33     dp[O]=0;
34     rep(i,1,n)
35     {
36         if(v[i]>=0)
37             per(j,maxm-1,v[i])
38                 dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
39         else
40             rep(j,0,maxm-1+v[i])
41                 dp[j]=max(dp[j],dp[j-v[i]]+w[i]);
42     }
43     int ans=0;
44     rep(i,O,maxm-1)
45         if(dp[i]>=0)
46             ans=max(ans,dp[i]+i-O);
47     cout<<ans;
48     return 0;
49 }

原文地址:https://www.cnblogs.com/ibilllee/p/9219019.html

时间: 2024-10-16 22:45:29

Cow Exhibition [POJ2184] [DP] [背包的负数处理]的相关文章

POJ2184 Cow Exhibition 【01背包】

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9383   Accepted: 3601 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to t

poj 2184 Cow Exhibition(01背包)

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10882   Accepted: 4309 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to

poj 2184 Cow Exhibition(dp之01背包变形)

Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to the public that they are both smart and fun. In order to do this, Bessie has organized an exhibitio

poj2184 Cow Exhibition(p-01背包的灵活运用)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to the public that they ar

POJ 2184:Cow Exhibition(01背包变形)

题意:有n个奶牛,每个奶牛有一个smart值和一个fun值,可能为正也可能为负,要求选出n只奶牛使他们smart值的和s与fun值得和f都非负,且s+f值要求最大. 分析: 一道很好的背包DP题,我们将smart值当作物品的体积,将fun值当作物品的价值,每个物品只能取一次,我们求对于每个背包体积求恰好装满该体积时价值和的最大值,也就是当所选奶牛smart值为某个值时,fun值的和的最大值.然后对于每个非负背包体积(smart值的和),判断对应最大价值(fun值的和)是否非负,如果非负说明这是一

POJ 2184 Cow Exhibition (变种01背包)

题意:有一些奶牛,他们有一定的s值和f值,这些值有正有负,最后让保证s的和为非负且f的和为非负的情况下,s+f的最大值. 思路:很明显的就是取与不取的问题,对于这类问题的第一想法就是背包,但是这道题目很明显与一般的背包不同,因为有负数,但是联想到以前也有这种将负数存入下标的情况,那就是将数组开大,换一种存法 我们用dp[i]存放每个s[i]能得到的最佳F,那么我们就可以根据s[i]的取值采取两种不同的01背包取法,在取完之后,然后再根据背包的有无再去求得最佳答案即可 #include <stdi

poj(2184)——Cow Exhibition(01背包变形)

其实我想说这道题我觉得我自己并没有深刻的理解.但是今天做了一下,先把现在的想法记录下来 . 题目的大致意思是: 有N头牛,每头牛都有一个s值代表智商值,f值代表它的幽默值. 然后问你智商值和幽默值的总和值最大是多少,其中必须保证智商值的和与幽默值的和为非负数. 一开始我想到的也是01背包,但是这里还有负值,于是我就没有办法了.于是学习到了一个相当于把坐标平移的方法. 因为这里有-1000到0的值,于是我们把它们全都移到右边去,于是变成了非负值0-2000. 解法: 01背包. 但是要注意的是当x

poj2184 Cow Exhibition p-01背包的灵活运用

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=2184 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to the public that they ar

poj 2184 - Cow Exhibition (01背包) 解题报告

Cow Exhibition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10279   Accepted: 4016 Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - Cows with Guns by Dana Lyons The cows want to prove to