Cow exhibition
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 9429 |
|
Accepted: 3624 |
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 is both smart and fun. In order to does this, Bessie have organized an exhibition that would be put on by the cows. She has given each of the N (1 <= n <=) Cows a thorough interview and determined the values for each cow:the SM Artness si ( -1000 <= si <=) of the cow and the funness fi ( -1000 <= fi <=) of the cow.
Bessie must choose which cows she wants to bring to her exhibition. She believes the total smartness TS of the group are the sum of the Si ' s and, likewise, the total funness TF of the GR OUP 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 n On-negative (since she must also show that the cows is 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.
Output
* Line 1:one integer:the optimal sum of TS and TF such that both TS and TF is non-negative. If no subset of the cows has non-negative TS and non-negative TF, print 0.
Sample Input
5-5 78-66-32 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 ten, but the new value of TF would was negative, so it's not
Allowed.
Forgive my patriotism, so quiet mind to see the problem, directly on the online Baidu Knot report to find test instructions, the basic online is vague, like farting.
Test instructions: S and F are two properties of cattle, which tells you the two properties of n cows, and then asks you to select a random number of cows, so that the S and F values of all the cows are added up to the maximum, while the S value of all the cows is not less than 0, and the F value is not less than 0.
Analysis: As a 01 backpack to do is to take one of the properties as capacity, so down, a certain capacity to get the maximum value of the best.
For example, s as a capacity, dp[x] meaning is: The S value is X when the F value of the most
But the S value has less than 0, then we think about how small the minimum is good to do, think of it!
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespaceStd;intDp[222222]; / /Minimum is -10w, then the whole array to leave more than 10W of space to negative.int Main(void){ intN,M,I,J,K,L,A,B,X=111111; //x Location of Mark 0 intMax,Min; / /maximum minimum boundary for easy traversal of data in the range of values already obtained while(scanf("%d",&N)!=Eof) { for(I=0;I<222222;I++) {Dp[I]=-999999; }Max=Min=X;Dp[X]=0; for(I=0;I<N;I++) { scanf("%d%d",&A,&B); if(A<0) { for(J=Min;J<=Max;J++) / /Negative to small to large, 01 backpack { if(Dp[J+A]<Dp[J]+B) {Dp[J+A]=Dp[J]+B; } }Min+=A; }Else { for(J=Max;J>=Min;J--) / /positive to from big to small, 01 backpack { if(Dp[J+A]<Dp[J]+B) {Dp[J+A]=Dp[J]+B; } }Max+=A; } }L=0;K=X; while(K<=Max)//Find all combinations with s value greater than 0 { if(Dp[K]>=0&&L<K-X+Dp[K])//f is also greater than 0 {L=K-X+Dp[K]; }K++; } printf("%d\ n",L); } return 0;}
Poj--2184--cow exhibition--01 Backpack