首页 > 代码库 > poj2184 Cow Exhibition
poj2184 Cow Exhibition
思路:
dp+滚动数组。
类似01背包。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 6 const int INF = 0x3f3f3f3f; 7 const int t = 100000; 8 int a[105], b[105], dp[2][200005], n, p, q; 9 int solve(int n) 10 { 11 for (int i = 0; i < 2; i++) 12 { 13 for (int j = -t; j <= t; j++) 14 { 15 dp[i][j + t] = -INF; 16 } 17 } 18 for (int i = 0; i < 2; i++) 19 dp[i][a[i] + t] = b[i]; 20 for (int i = 1; i < n; i++) 21 { 22 for (int j = -t; j <= t; j++) 23 { 24 dp[i & 1][j + t] = max(dp[(i - 1) & 1][j + t], dp[i & 1][j + t]); 25 if (j + t - a[i] < 0 || j + t - a[i] > 200000) 26 continue; 27 dp[i & 1][j + t] = max(dp[i & 1][j + t], dp[(i - 1) & 1][j + t - a[i]] + b[i]); 28 } 29 } 30 int ans = -INF; 31 for (int j = 0; j <= t; j++) 32 { 33 ans = max(ans, dp[(n - 1) & 1][j + t] >= 0 ? j + dp[(n - 1) & 1][j + t] : -INF); 34 } 35 return ans; 36 } 37 38 int main() 39 { 40 cin >> n; 41 int cnt = 0; 42 for (int i = 0; i < n; i++) 43 { 44 cin >> p >> q; 45 if (p < 0 && q < 0) 46 continue; 47 a[cnt] = p; 48 b[cnt++] = q; 49 } 50 int ans = solve(cnt); 51 if (ans <= -INF) 52 cout << "0" << endl; 53 else 54 cout << ans << endl; 55 return 0; 56 }
poj2184 Cow Exhibition
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。