poj1276(dp)

题目链接:http://poj.org/problem?id=1276

 

Cash Machine
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 29827   Accepted: 10733

Description

A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The machine uses exactly N distinct bill denominations, say Dk, k=1,N, and for each denomination Dk the machine has a supply of nk bills. For example, 

N=3, n1=10, D1=100, n2=4, D2=50, n3=5, D3=10 

means the machine has a supply of 10 bills of @100 each, 4 bills of @50 each, and 5 bills of @10 each. 

Call cash the requested amount of cash the machine should deliver and write a program that computes the maximum amount of cash less than or equal to cash that can be effectively delivered according to the available bill supply of the machine. 

Notes: 
@ is the symbol of the currency delivered by the machine. For instance, @ may stand for dollar, euro, pound etc. 

Input

The program input is from standard input. Each data set in the input stands for a particular transaction and has the format: 

cash N n1 D1 n2 D2 ... nN DN 

where 0 <= cash <= 100000 is the amount of cash requested, 0 <=N <= 10 is the number of bill denominations and 0 <= nk <= 1000 is the number of available bills for the Dk denomination, 1 <= Dk <= 1000, k=1,N. White spaces can occur freely between the numbers in the input. The input data are correct. 

Output

For each set of data the program prints the result to the standard output on a separate line as shown in the examples below. 

Sample Input

735 3  4 125  6 5  3 350
633 4  500 30  6 100  1 5  0 1
735 0
0 3  10 100  10 50  10 10

Sample Output

735
630
0
0

Hint

The first data set designates a transaction where the amount of cash requested is @735. The machine contains 3 bill denominations: 4 bills of @125, 6 bills of @5, and 3 bills of @350. The machine can deliver the exact amount of requested cash. 

In the second case the bill supply of the machine does not fit the exact amount of cash requested. The maximum cash that can be delivered is @630. Notice that there can be several possibilities to combine the bills in the machine for matching the delivered cash. 

In the third case the machine is empty and no cash is delivered. In the fourth case the amount of cash requested is @0 and, therefore, the machine delivers no cash.

Source

 
大意:给你n种货币的数量及价值,拼凑出不大于cash的最大金额。
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <stack>
 5 #include <queue>
 6 #include <map>
 7 #include <set>
 8 #include <vector>
 9 #include <cmath>
10 #include <algorithm>
11 using namespace std;
12 #define ll long long
13 const double eps = 1e-6;
14 const double pi = acos(-1.0);
15 const int INF = 0x3f3f3f3f;
16 const int MOD = 1000000007;
17 
18 int dp[100005];
19 int V,n,k[1005],w[1005];
20 
21 int main ()
22 {
23     while (scanf ("%d",&V)==1)
24     {
25         scanf ("%d",&n);
26         for (int i=1; i<=n; i++)
27             scanf ("%d%d",&k[i],&w[i]);
28         memset(dp, 0, sizeof(dp));
29         if (!V||!n){printf ("0\n");continue;}
30         int maxx=0;dp[0]=1;
31         for (int i=1; i<=n; i++)
32         {
33             for (int j=maxx; j>=0; j--)
34             {
35                 if (dp[j])
36                 {
37                     for (int l=1; l<=k[i]; l++)
38                     {
39                         if (j+l*w[i]>V) continue;
40                         dp[j+l*w[i]]=1;
41                         if (maxx<j+l*w[i]) maxx=j+l*w[i];
42                     }
43                 }
44             }
45         }
46         printf ("%d\n",maxx);
47     }
48     return 0;
49 }
View Code

暴力水过,另附二进制优化代码,76msAC。

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #define Max(a, b)   a>b?a:b
 4 int dp[100005] ;
 5 int val[105] ;
 6 int main()
 7 {
 8     int cash, n, v, g, i, j, count ;
 9     while(~scanf("%d%d", &cash, &n))
10     {
11         if(!cash||!n)
12         {
13             while(n--)
14                 scanf("%d%d", &g, &v) ;
15             printf("0\n") ;
16             continue ;
17         }
18         count = 0 ;
19         memset(dp, 0, sizeof(dp)) ;
20         while(n--)
21         {
22             scanf("%d%d", &g, &v) ;
23             //二进制优化
24             i = 1 ;
25             while(g>=i)
26             {
27                 val[count++] = i * v ;
28                 g -= i ;
29                 i *= 2 ;
30             }
31             if(g)   val[count++] = v * g ;
32         }
33         //01背包求解
34         for(i=0; i<count; i++)
35         {
36             for(j=cash; j>=val[i]; j--)
37             {
38                 dp[j] = Max(dp[j], dp[j-val[i]]+val[i]) ;
39             }
40         }
41         printf("%d\n", dp[cash]) ;
42     }
43     return 0 ;
44 }
View Code

 

posted @ 2015-08-21 21:27  敲敲代码,打打酱油  阅读(122)  评论(0编辑  收藏  举报