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

POJ 1742

时间:2014-05-14 11:52:46      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   class   code   c   

Coins
Time Limit: 3000MS   Memory Limit: 30000K
Total Submissions: 27580   Accepted: 9335

Description

People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found there were some coins.He decided to buy a very nice watch in a nearby shop. He wanted to pay the exact price(without change) and he known the price would not more than m.But he didn‘t know the exact price of the watch. 
You are to write a program which reads n,m,A1,A2,A3...An and C1,C2,C3...Cn corresponding to the number of Tony‘s coins of value A1,A2,A3...An then calculate how many prices(form 1 to m) Tony can pay use these coins. 

Input

The input contains several test cases. The first line of each test case contains two integers n(1<=n<=100),m(m<=100000).The second line contains 2n integers, denoting A1,A2,A3...An,C1,C2,C3...Cn (1<=Ai<=100000,1<=Ci<=1000). The last test case is followed by two zeros.

Output

For each test case output the answer on a single line.

Sample Input

3 10
1 2 4 2 1 1
2 5
1 4 2 1
0 0

Sample Output

8
4

Source

 
定义dp[i]为目前为止装满i容量最多剩余多少个当前面值的
bubuko.com,布布扣
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 const int MAX_N = 105;
 9 const int MAX_M = 1e5 + 7;
10 int N, M;
11 int v[MAX_N], c[MAX_N];
12 int dp[MAX_M];
13 
14 void solve() {
15         dp[0] = 0;
16         for(int i = 1; i <= N; ++i) {
17                 for(int j = 0; j <= M; ++j) {
18                         if(dp[j] >= 0) {
19                                 dp[j] = c[i];
20                         }
21                         else if(j < v[i] || dp[j - v[i]] <= 0) {
22                                 dp[j] = -1;
23                         } else {
24                                 dp[j] = dp[j - v[i]] - 1;
25                         }
26 
27                 }
28         }
29 
30 
31         int ans = 0;
32         for(int i = 1; i <= M; ++i) {
33                 ans += dp[i] >= 0;
34                 //printf("%d ",dp[i]);
35         }
36 
37         printf("%d\n", ans);
38 }
39 int main()
40 {
41    // freopen("sw.in", "r", stdin);
42     while(~scanf("%d%d", &N, &M) && (N + M)) {
43             memset(dp, -1, sizeof(dp));
44             for(int i = 1; i <= N; ++i) {
45                     scanf("%d", &v[i]);
46             }
47             for(int i = 1; i <= N; ++i) {
48                     scanf("%d", &c[i]);
49             }
50 
51             solve();
52     }
53     //cout << "Hello world!" << endl;
54     return 0;
55 }
View Code

 

POJ 1742,布布扣,bubuko.com

POJ 1742

标签:des   style   blog   class   code   c   

原文地址:http://www.cnblogs.com/hyxsolitude/p/3726867.html

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