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 -5Sample Output
8HintOUTPUT 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;}
相关文章推荐
- C++动态规划之最长公子序列实例
- C++动态规划之背包问题解决方法
- C#使用动态规划解决0-1背包问题实例分析
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- 动态规划
- C++ 动态规划
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- DP(动态规划) 解游轮费用问题
- Android对px和dip进行尺寸转换的方法
- 动态规划的用法——01背包问题
- 动态规划的用法——01背包问题
- 《收集苹果》 动态规划入门
- 《DNA比对》蓝桥杯复赛试题
- 《背包问题》 动态规划
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207