Cash machinetime limit:1000msmemory limit:10000kbthis problem would be judged onPKU. Original id:1276
64-bit integer IO format: %lld Java class name: Main 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.
InputThe 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.
OutputFor 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
HintThe 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 is not fit the exact amount of cash requested. The maximum cash that can was delivered is @630. Notice that there can is several possibilities to combine the bills of the machine for matching the delivered cash.
In the third case, the machine was empty and no cash is delivered. In the fourth case the amount of cash requested are @0 and, therefore, the machine delivers no cash.SourceSoutheastern Europe 2002
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <vector>6 using namespacestd;7 Const intMAXN =100010;8 intdp[maxn],val[maxn],num[maxn],n,m;9 intMain () {Ten while(~SCANF ("%d%d",&n,&m)) { One for(inti =0; I < m; ++i) Ascanf"%d%d", num+i,val+i); -Memset (DP,0,sizeofDP); - for(inti =0; I < m; ++i) { the intCNT =1; - while(Num[i]) { - if(CNT > Num[i]) cnt =Num[i]; -Num[i]-=CNT; + for(intj = N; J >= Cnt*val[i]; --j) -DP[J] = max (dp[j],dp[j-cnt*val[i]]+cnt*val[i]); +CNT <<=1; A } at } -printf"%d\n", Dp[n]); - } - return 0; -}
View Code
Problem solving: Multiple backpacks can also be done with a complete backpack, as long as the constraints.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <vector>6 using namespacestd;7 Const intMAXN =100010;8 intdp[maxn],val[maxn],num[maxn],cnt[maxn],n,m;9 intMain () {Ten while(~SCANF ("%d%d",&n,&m)) { One for(inti =0; I < m; ++i) Ascanf"%d%d", num+i,val+i); -Memset (DP,0,sizeofDP); - for(inti =0; I < m; ++i) { thememset (CNT,0,sizeofCNT); - for(intj = Val[i]; J <= N; ++j) - if(Dp[j-val[i]] + val[i]> dp[j] && Cnt[j-val[i]] <Num[i]) { -DP[J] = dp[j-val[i]]+Val[i]; +CNT[J] = Cnt[j-val[i]] +1; - } + } Aprintf"%d\n", Dp[n]); at } - return 0; -}
View Code
POJ 1276 Cash Machine