码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 234F 简单dp

时间:2015-02-12 19:51:53      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

//CodeForces 234F

 1 #include "iostream"
 2 #include "cstdio"
 3 #include "cstring"
 4 #include "algorithm"
 5 using namespace std;
 6 const int INF = 0x3f3f3f3f;
 7 int dp[210][40010][2];
 8 int n, a, b;
 9 int h[210], c[210], s[210];
10 
11 int main()
12 {
13     freopen("input.txt", "r", stdin);
14     freopen("output.txt", "w", stdout);
15     int i, j, k;
16     scanf("%d%d%d", &n, &a, &b);
17     for(i = 1; i<=n; ++i) {
18         scanf("%d", &h[i]);
19         c[i] = min(h[i-1], h[i]);
20         s[i] = s[i-1] + h[i];
21     }
22     memset(dp, 0x3f, sizeof(dp));
23     dp[0][0][0] = dp[0][0][1] = 0;
24     for(i = 1; i <= n; ++i) {
25         for(j = 0; j<=s[i]; ++j) {
26             if(j > a)
27                 break;
28             if(s[i] - j > b)
29                 continue;
30             if(j >= h[i])
31                 dp[i][j][0] = min(dp[i-1][j - h[i]][0], dp[i-1][j - h[i]][1] + c[i]);
32             dp[i][j][1] = min(dp[i-1][j][0] + c[i], dp[i-1][j][1]);
33         }
34     }
35     int res = INF;
36     for(j = 0; j<=a; ++j) {
37         for(k = 0; k<=1; ++k)
38             res = min(res, dp[n][j][k]);
39     }
40     if(res==INF)
41         printf("-1");
42     else
43         printf("%d\n", res);
44 }

 

CodeForces 234F 简单dp

标签:

原文地址:http://www.cnblogs.com/AC-Phoenix/p/4288604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!