POJ 2184 Cow Exhibition (想法题&双变量0-1背包)
来源:互联网 发布:汽车行业cae软件 编辑:程序博客网 时间:2024/05/17 18:49
http://poj.org/problem?id=2184
dp[s]表示当TS=s时,TF的最大值。
背包容量?——我们要找到它的最大值,不断分开累加正s和负s即可。
if (s > 0){for (j = Max; j >= Min; --j)dp[j + s] = max(dp[j + s], dp[j] + f);///dp[s]表示当TS=s时,TF的最大值Max += s;}else{if (f <= 0) continue; ///剪枝for (j = Min; j <= Max; ++j)dp[j + s] = max(dp[j + s], dp[j] + f);///dp[s]表示当TS=s时,TF的最大值Min += s;}
完整代码:
/*32ms,1168KB*/#include<cstdio>#include<cstring>#include<algorithm>using namespace std;int dp[200005];int main(){int n, i, j, s, f, Min, Max;memset(dp, -0x3f, sizeof(dp));dp[Min = Max = 100000] = 0;scanf("%d", &n);for (i = 0; i < n; ++i){scanf("%d%d", &s, &f);if (s > 0){for (j = Max; j >= Min; --j)dp[j + s] = max(dp[j + s], dp[j] + f);///dp[s]表示当TS=s时,TF的最大值Max += s;}else{if (f <= 0) continue; ///剪枝for (j = Min; j <= Max; ++j)dp[j + s] = max(dp[j + s], dp[j] + f);///dp[s]表示当TS=s时,TF的最大值Min += s;}}int ans = 0;for (i = 100000; i <= Max; ++i)if (dp[i] >= 0) ans = max(ans, dp[i] + (i - 100000));printf("%d\n", ans);return 0;}
0 0
- POJ 2184 Cow Exhibition (想法题&双变量0-1背包)
- POJ 2184 Cow Exhibition 0-1背包
- POJ 2184 Cow Exhibition(0-1背包)
- POJ 2184 Cow Exhibition(0-1背包,负化正)
- POJ 2184 Cow Exhibition (01背包变形 + 技巧 好题)
- POJ 2184 Cow Exhibition [dp 背包]
- POJ 2184 Cow Exhibition 背包问题
- poj 2184 Cow Exhibition(01背包)
- POJ 2184 Cow Exhibition ( 01背包变形 )
- POJ 2184 Cow Exhibition 01背包
- poj 2184 Cow Exhibition(01背包变形)
- POJ 2184 Cow Exhibition(DP:01背包)
- POJ 2184 Cow Exhibition(01背包变形)
- poj 2184 Cow Exhibition 01背包变形
- poj 2184 Cow Exhibition 【另类01背包】
- POJ--2184--Cow Exhibition--01背包
- POJ-2184-01背包变形-Cow Exhibition
- POJ 2184 Cow Exhibition 01背包
- 支持向量机1: Maximum Margin Classifier —— 支持向量机简介
- java设计模式8——代理模式(Proxy)
- 【索引】Designing Efficient Algorithms:Examples
- PowerDesigner生成Oracle表名带有引号的解决方法
- 双向一对多映射
- POJ 2184 Cow Exhibition (想法题&双变量0-1背包)
- IM服务器的架构
- 网页google地图加载(一)
- js 日期选择判断结束时间大于开始时间
- oracle字符集
- u-boot-2012.10启动流程
- NFC功能介绍及MifareClassic类型的读取数据例子
- 聚集索引和非聚集索引(整理)
- “哦……你的修为才上清中期,但竟然能看透我的修为