poj 2184 Cow Exhibition dp
来源:互联网 发布:淘宝品控申诉 编辑:程序博客网 时间:2024/06/05 02:32
题目
题目链接:http://poj.org/problem?id=2184
题目来源:《挑战》练习题
简要题意:
N 头牛有Si 值和Fi 值,求一牛子集令Si 之和与Fi 之和均非负且两者之和最大。数据范围:
1⩽N⩽100;−1000⩽Si⩽1000;−1000⩽Fi⩽1000
题解
首先肯定是用和去
dp 的,但是显然下标搞成两个和的话得TLE。可以弄成
dp[i][j] 是前i 个Si 为j 时Fi 之和的最大值。对上一层每个合法的值直接进行转移就行了。
实现
需要有标记值为非法,另外要注意中间变成负的是可以的,因而规模是
200000 。数组比较大,我选择了开滚动数组,然后还搞了个边界的优化。
下标负数的话搞个偏移量就行了,取
100000 就行了。
代码
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define pb push_back#define mp make_pair#define all(x) (x).begin(),(x).end()#define sz(x) ((int)(x).size())#define fi first#define se secondusing namespace std;typedef long long LL;typedef vector<int> VI;typedef pair<int,int> PII;LL powmod(LL a,LL b, LL MOD) {LL res=1;a%=MOD;for(;b;b>>=1){if(b&1)res=res*a%MOD;a=a*a%MOD;}return res;}// headconst int N = 105;const int M = 2E5+5;const int NINF = -1E9;const int OFFSET = 1E5;int dp[2][M];PII a[N];int main(){ for (int i = 0; i < M; i++) { dp[0][i] = dp[1][i] = NINF; } int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].fi, &a[i].se); } int l = OFFSET, r = OFFSET, pre = 0, cur = 1; dp[pre][OFFSET] = 0; for (int i = 0; i < n; i++) { for (int j = l; j <= r; j++) { dp[cur][j] = dp[pre][j]; } for (int j = l; j <= r; j++) { if (dp[pre][j] == NINF) continue; dp[cur][j+a[i].fi] = max(dp[cur][j+a[i].fi], dp[pre][j]+a[i].se); } l = min(l, l+a[i].fi); r = max(r, r+a[i].fi); swap(cur, pre); } int ans = 0; for (int i = OFFSET; i <= r; i++) { if (dp[pre][i] >= 0) { ans = max(ans, i-OFFSET+dp[pre][i]); } } printf("%d\n", ans); return 0;}
0 0
- poj 2184 Cow Exhibition dp
- POJ 2184 Cow Exhibition [dp 背包]
- POJ 2184 Cow Exhibition(DP:01背包)
- POJ 2184 Cow Exhibition(DP)
- POJ 2184 Cow Exhibition (dp)
- [POJ 2184]Cow Exhibition[DP][01背包]
- poj 2184 Cow Exhibition
- POJ 2184 Cow Exhibition
- Poj 2184 Cow Exhibition
- poj 2184 Cow Exhibition
- poj 2184 Cow Exhibition
- POJ 2184 Cow Exhibition
- POJ-2184-Cow Exhibition
- POJ 2184 Cow Exhibition
- poj 2184 Cow Exhibition
- poj 2184 Cow Exhibition
- poj 2184 Cow Exhibition
- poj 2184--Cow Exhibition
- 一张图说懂java中 private default protected public 的区别
- eclipse, Log4j配置(真心的详细~)
- git reset的三种模式
- PHP
- HDU 2918 Tobo or not Tobo
- poj 2184 Cow Exhibition dp
- POJ3292Semi-prime H-numbers(筛选法)
- js获取网站绝对路径
- Zabbix自定义监控网站服务是否能够正常响应
- ANDROID自定义视图——onMeasure流程,MeasureSpec详解
- 在QT中以日历的形式选择时间
- 帧动画、补间动画(代码或xml)
- NoClassException:org/apache/commons/lang/exception/NestableRuntimeException
- Generative classfiers And Discrimivative