Cash Machine
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 30973 |
|
Accepted: 11161 |
Description
A Bank plans to install a machine for cash withdrawal. The machine was 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 have a supp Ly 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 ten 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 so can be effectively delivered according for the available bill supply of the machine.
Notes:
@ is the symbol of the currency delivered by the machine. For instance, @ could stand for dollar, euro, pound etc.
Input
The program input was from standard input. Each data set in the input stands for a particular transaction and have the format:
Cash N N1 D1 n2 D2 ... nN DN
where 0 <= cash <= 100000 is the amount of cash requested, 0 <=n <= are the number of Bill denominations and 0 <= nk <= are the number of available bills for the DK denomination, 1 <= dk <=, k=1,n. White space s can occur freely between the numbers in the input. The input data is 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 Belo W.
Sample Input
735 3 4 6 5 3 350633 4 ten 6 1 5 0 1735 3
Sample Output
73563000
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intCash,n;inta[ the],m[ the];intdp[100005];intMain () { while(SCANF ("%d%d", &cash,&n)! =EOF) {memset (DP,-1,sizeof(DP)); for(intI=0; i<n;i++) {scanf ("%d%d",&m[i],&A[i]); } dp[0]=0; for(intI=0; i<n;i++) for(intj=0; j<=cash;j++) if(dp[j]>=0) Dp[j]=M[i]; Else if(j<a[i]| | dp[j-a[i]]<0) Dp[j]=-1; ElseDp[j]=dp[j-a[i]]-1; for(inti=cash;i>=0; i--) if(dp[i]>=0) {printf ("%d\n", i); Break; } } return 0;}
Convert to multiple backpacks
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;intCash,n;intw[10005];intdp[100005];intMain () { while(SCANF ("%d%d", &cash,&n)! =EOF) {memset (DP,0,sizeof(DP)); intCnt=0; for(intI=0; i<n;i++) { intnum,v; scanf ("%d%d",&num,&v); intE=1; while(e<num) {w[cnt++]=e*v; Num-=e; E<<=1; } if(num>0) w[cnt++]=num*v; } for(intI=0; i<cnt;i++) for(intj=cash;j>=w[i];j--) Dp[j]=max (dp[j],dp[j-w[i]]+W[i]); printf ("%d\n", Dp[cash]); } return 0;}
POJ1276 (multiple backpack)