Test instructions: There are some cows, they have a certain S value and f value, these values have a positive negative, and finally let the guaranteed s and for non-negative and F, and for non-negative cases, the maximum value of s+f.
Idea: It is obvious that the problem is taken and not taken, the first idea of this kind of problem is the backpack, but this topic is obviously different from the general backpack, because there are negative, but Lenovo to the former also have this negative deposit in the subscript, that is, the array opened large, in exchange for a kind of deposit method
We use Dp[i] to store each s[i] can get the best f, then we can take the value of the s[i] for two different 01-pack, after the take, and then according to the backpack to find out the best answer can be
#include <stdio.h>#include <string.h>#include <algorithm>using namespace STD;intdp[200005];Const intINF =1<< -;intMain () {intn,s[ $],f[ $],i,j,ans; while(~scanf("%d", &n)) { for(i =0; i<=200000; i++) Dp[i] =-inf; dp[100000] =0; for(i =1; i<=n; i++)scanf("%d%d", &s[i],&f[i]); for(i =1; i<=n; i++) {if(s[i]<0&& f[i]<0)Continue;if(s[i]>0) { for(j =200000; j>=s[i]; j--)//If s[i] is an integer, then we carry the backpack from a large, small direction if(dp[j-s[i]]>-inf) Dp[j] = max (dp[j],dp[j-s[i]]+f[i]); }Else{ for(j = s[i]; j<=200000+s[i]; J + +)//For negative numbers you need to reverse if(dp[j-s[i]]>-inf) Dp[j] = max (dp[j],dp[j-s[i]]+f[i]); }} ans =-inf; for(i =100000; i<=200000; i++) {if(dp[i]>=0) ans = max (ans,dp[i]+i-100000); }printf("%d\n", ans); }return 0;}
POJ 2184 Cow Exhibition (variant 01 backpack)