EOJ 1494 Coins

http://www.acm.cs.ecnu.edu.cn/problem.php?problemid=1494

poj 1742 http://poj.org/problem?id=1742

这题是一个部分背包,本来想过用二分的方法变成一个0-1背包,时间复杂度为(nlogV*V),但是还是超时了,所以要优化成(nV)的才能过

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <set>
 8 using namespace std;
 9  
10 int dp[100005], cou[100005];
11 int n, m;
12 int a[105], b[105];
13 
14 int k;
15  
16 int main()
17 {
18     while (cin >> n >> m)
19     {
20         for (int i = 0; i < n; i++)
21             scanf("%d", &a[i]);
22         for (int i = 0; i < n; i++)
23             scanf("%d", &b[i]);
24         memset(dp, 0, sizeof(dp));
25         dp[0] = 1;    
26         for (int i = 0; i < n; i++)
27         {
28             memset(cou, 0, sizeof(cou));
29             for (int j = a[i]; j <= m; j++)
30                 if (dp[j - a[i]] && !dp[j] && cou[j - a[i]] < b[i])
31                 {
32                     cou[j] = cou[j - a[i]] + 1;
33                     dp[j] = 1;
34                 }
35         }
36         int cnt = 0;
37         for (int i = 1; i <= m; i++)
38             if (dp[i])
39                 cnt++;
40         cout << cnt << endl;    
41     }
42     return 0;
43 }
View Code

 

 

posted on 2013-07-16 13:25  KimKyeYu  阅读(244)  评论(0编辑  收藏  举报

导航