# Cow Exhibition POJ - 2184

### 题目地址：https://vjudge.net/problem/POJ-2184

dp[i][j] = max(dp[i - 1][j - s[i]] + f[i], dp[i - 1][j])。在实际操作的时候可以将第一维去掉，进行空间上的优化。

 1 #include<iostream>
 2 #include<algorithm>
3 using namespace std;
4 #define rep(i,j,k) for(int i = (j); i <= (k); i++)
5 #define per(i,j,k) for(int i = (j); i >= (k); i--)
6 #define mv (int)1e5
7 #define N 105
8 #define inf (1LL << 30) - 1
9 #define maxn (int)2e5 + 10
10
11 int dp[maxn];
12 int s[N], f[N];
13 int n;
14
15 void input(){
16
17     rep(i, 0, maxn - 1) dp[i] = -inf;
18
19     cin >> n;
20     rep(i, 1, n) cin >> s[i] >> f[i];
21 }
22
23 void work(){
24
25     dp[0 + mv] = 0;
26
27     rep(i, 1, n){
28         if (s[i] > 0){
29             per(o, (int)1e5, (int)(-1e5 + s[i]))
30                 dp[o + mv] = max(dp[o + mv], dp[o - s[i] + mv] + f[i]);
31         }
32         else{
33             rep(o, (int)-1e5, (int)(1e5 + s[i]))
34                 dp[o + mv] = max(dp[o + mv], dp[o - s[i] + mv] + f[i]);
35         }
36     }
37
38     int ans = 0;
39     rep(i, 0, (int)1e5){
40         if (dp[i + mv] >= 0) ans = max(ans, i + dp[i + mv]);
41     }
42
43     cout << ans << endl;
44 }
45
46 int main(){
47
48     ios::sync_with_stdio(false);
49     cin.tie(0);
50     input();
51     work();
52
53     return 0;
54 }

posted @ 2019-07-10 14:54  SummerMingQAQ  阅读(59)  评论(0编辑  收藏