描述
http://poj.org/problem?id=2184
n只奶牛,每只都有智商s_i和情商f_i,取出若干只,保证智商之和与情商之和都不为负的情况下,让两者之和最大.
Cow Exhibition
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11635 | Accepted: 4610 |
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 exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
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 exhibition that will be put on by the cows. She has given each of the N (1 <= N <= 100) cows a thorough interview and determined two values for each cow: the smartness Si (-1000 <= Si <= 1000) of the cow and the funness Fi (-1000 <= Fi <= 1000) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes that the total smartness TS of the group is the sum of the Si's and, likewise, the total funness TF of the group is the sum of the Fi's. Bessie wants to maximize the sum of TS and TF, but she also wants both of these values to be non-negative (since she must also show that the cows are well-rounded; a negative TS or TF would ruin this). Help Bessie maximize the sum of TS and TF without letting either of these values become negative.
Input
* Line 1: A single integer N, the number of cows
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
* Lines 2..N+1: Two space-separated integers Si and Fi, respectively the smartness and funness for each cow.
Output
* Line 1: One integer: the optimal sum of TS and TF such that both TS and TF are non-negative. If no subset of the cows has non-negative TS and non- negative TF, print 0.
Sample Input
5 -5 7 8 -6 6 -3 2 1 -8 -5
Sample Output
8
Hint
OUTPUT DETAILS:
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
Bessie chooses cows 1, 3, and 4, giving values of TS = -5+6+2 = 3 and TF
= 7-3+1 = 5, so 3+5 = 8. Note that adding cow 2 would improve the value
of TS+TF to 10, but the new value of TF would be negative, so it is not
allowed.
Source
分析
是一个关于取与不取得问题,而当智商之和一定时,情商之和越大越好,所以类似01背包,用智商之和作为dp数组下标,dp[i]表示智商之和为i时,情商的最大值,动规结束后扫一遍,找到符合要求的最有答案即可.
但是注意这里用智商之和作为下标时,智商之和有可能为负,所以用idx把整个数组向右移.统计智商之和最小的值,向右移动这个值即可.则dp[i]表示的是智商之和为(i-idx)的情商最大值.
注意:
1.开始时dp数组要全部赋为赋值.
2.赋的值不能使-0x7fffffff,因为可能会和负的情商相加...(貌似不是第一次犯这种错误了0.0)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <cstdio> 2 #include <algorithm> 3 #define for1(i,a,n) for(int i=(a);i<=(n);i++) 4 #define read(a) a=getnum() 5 using namespace std; 6 7 const int INF=1<<27; 8 int n,mins,maxs; 9 int s[100+5],f[100+5]; 10 int dp[1000*100*2+5]; 11 12 inline int getnum(){int r=0,k=1;char c;for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')k=-1;for(;c>='0'&&c<='9';c=getchar())r=r*10+c-'0';return r*k;} 13 14 void solve() 15 { 16 int idx=mins; 17 int range=idx+maxs; 18 for1(i,0,range) dp[i]=-INF; 19 dp[idx]=0; 20 for1(i,1,n) 21 { 22 if(s[i]>=0) 23 { 24 for(int j=range;j>=s[i];j--) 25 { 26 dp[j]=max(dp[j],dp[j-s[i]]+f[i]); 27 } 28 } 29 else 30 { 31 for(int j=0;j-s[i]<=range;j++) 32 { 33 dp[j]=max(dp[j],dp[j-s[i]]+f[i]); 34 } 35 } 36 } 37 int ans=-INF; 38 for(int i=idx;i<=range;i++) 39 { 40 if(dp[i]<0) continue; 41 ans=max(ans,i-idx+dp[i]); 42 } 43 printf("%d\n",ans); 44 } 45 46 void init() 47 { 48 read(n); 49 for1(i,1,n) 50 { 51 read(s[i]); 52 read(f[i]); 53 if(s[i]>0) maxs+=s[i]; 54 else mins-=s[i]; 55 } 56 } 57 58 int main() 59 { 60 #ifndef ONLINE_JUDGE 61 freopen("cow.in","r",stdin); 62 freopen("cow.out","w",stdout); 63 #endif 64 init(); 65 solve(); 66 #ifndef ONLINE_JUDGE 67 fclose(stdin); 68 fclose(stdout); 69 system("cow.out"); 70 #endif 71 return 0; 72 }