【HDOJ】2844 Coins

完全背包。

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 int a[105], c[105];
 5 int n, m;
 6 int dp[100005];
 7 
 8 int mymax(int a, int b) {
 9     return a>b ? a:b;
10 }
11 
12 void CompletePack(int c) {
13     int i;
14 
15     for (i=c; i<=m; ++i)
16         dp[i] = mymax(dp[i], dp[i-c]);
17 }
18 
19 void ZeroOnePack(int c) {
20     int i;
21 
22     for (i=m; i>=c; --i)
23         dp[i] = mymax(dp[i], dp[i-c]);
24 }
25 
26 void multipack(int c, int n) {
27     int k;
28     if (c*n >= m) {
29         CompletePack(c);
30         return ;
31     }
32     k = 1;
33     while (k < n) {
34         ZeroOnePack(k*c);
35         n -= k;
36         k *= 2;
37     }
38     ZeroOnePack(n*c);
39 }
40 
41 int main() {
42     int i, total;
43 
44     while (scanf("%d%d",&n,&m)!=EOF && (n||m)) {
45         memset(dp, 0, sizeof(dp));
46         dp[0] = 1;
47         for (i=1; i<=n; ++i)
48             scanf("%d", &a[i]);
49         for (i=1; i<=n; ++i)
50             scanf("%d", &c[i]);
51         for (i=1; i<=n; ++i)
52             multipack(a[i], c[i]);
53         total = 0;
54         for (i=1; i<=m; ++i)
55             if (dp[i])
56                 ++total;
57         printf("%d\n", total);
58     }
59 
60     return 0;
61 }

 

posted on 2014-05-26 21:38  Bombe  阅读(137)  评论(0编辑  收藏  举报

导航