您的位置:首页 > 其它

POJ 2184 - Cow Exhibition

2015-11-10 15:29 288 查看
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 foreach 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 TSand 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. 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
HintOUTPUT 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. 
大意:给出N对数字,要求取出一些数字,使得其和最大,切对应的两个分量和不为0;
思路:
定义dp[k][s]:前k对数字中,取得的s分量和为s时候,f分量和的最大值。
dp[k][s] = max(dp[k][s], dp[k-1][s-s[i]])
需要注意的是:
①s有可能为负,转换一下就行了;
②dp不做优化会很大,但是规划没有明确的方向。可以用dp[2][M],也是滚蛋数组,应为每次规划只依赖于前面,最好是用规划次数模2。
#include<cstdio>#include<algorithm>using namespace std;const int N = 100 + 5;const int M = 100000 + 5;const int INF = - (1 << INF);const int L = 100000;//dp[k][s]:select from the first k cows and get s, how much could f get  int dp[2][2 * M];inline int trans(const int x){return x + L;}int solve(const int n){int a, b, res = 0;memset(dp, -1, sizeof(dp));dp[0][trans(0)] = trans(0);for(int k = 1; k <= n; k++){scanf("%d%d", &a, &b);for(int s = -k * 1000; s <= k * 1000; s++){dp[k % 2][trans(s)] = dp[(k-1) % 2][trans(s)];if(s - a >= -k * 1000 && s - a <= k * 1000 && dp[(k-1) % 2][trans(s-a)] != -1)dp[k % 2][trans(s)] = max(dp[k % 2][trans(s)], dp[(k-1) % 2][trans(s-a)] + trans(b) - L);if(trans(s) >= L && dp[k % 2][trans(s)] >= L)res = max(res, dp[k % 2][trans(s)] + trans(s) - 2 * L);}}return res;}int main(){int n;scanf("%d", &n);printf("%d\n", solve(n));return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp 动态规划