HDU 5527 --- Too Rich (Greedy + SEARCH), hdu5527 --- too
Question Link
Problem DescriptionYou are a rich person, and you think your wallet is too heavy and full now. so you want to give me some money by buying a lovely pusheen sticker which costs pdollars from me. to make your wallet lighter, you decide to pay exactly p dollars by as your coins and/or banknotes as possible.
For example, if p = 17 and you have two $10 coins, four $5 coins, and eight $1 coins, you will pay it by two $5 coins and seven $1 coins. but this task is incredibly hard since you are too rich and the sticker is too expensive and pusheen is too lovely, please write a program to calculate the best solution.
InputThe first line contains an integer T indicating the total number of test cases. each test case is a line with 11 integers p, c1, c5, c10, c20, c50, c100, c200, c500, c1000, c2000, specifying the price of the pusheen sticker, and the number of coins and banknotes in each denomination. the number ci means how many coins/banknotes in denominations of I dollars in your wallet.
1 ≤ T ≤20000
0 ≤ p ≤ 109
0 ≤ ci ≤100000
OutputFor each test case, please output the maximum number of coins and/or banknotes he can pay for exactly p dollars in a line. if you cannot pay for exactly p dollars, please simply output '-1 '.
Sample Input317 8 4 2 0 0 0 0 0 0100 99 0 0 0 0 0 0 0 0 02015 9 8 7 6 5 4 3 2 1 0
Sample Output9-136 question: p indicates the amount of money to be paid. A series v [10] indicates the number of coins in the range of 1, 5, 10, 20, 50, 100,200,500,100, respectively, please use as many coins as possible to pay off the RMB p and output the number of coins. Idea: greedy, try to raise money with coins with a small face value, but it is likely that there are not enough coins with a small face value, so consider from the perspective of a large face value. Initialize a prefix and sum [12]. sum [I] indicates v [1] ~ V [I] the nominal value of the coin and, tmp = rest-sum [I-1], indicates how much the current nominal value of the coin should be paid, cn = tmp/v [I], that is, how many coins with the current nominal value should be taken out, if tmp % v [I]! = 0, so cn ++, because less than v [I] coins can not raise enough money; and for P = 50 coins for 20, 20, 20, 50, according to the greedy policy, 3 images and 20 images are 60, so 50 images are not taken, but 3 images and 20 images cannot be used to raise 50 RMB. Therefore, you must consider one more coin with each face value, for example, for such data: p = 1020 0 0 0 49 1 0 0 0 1 0; the Code is as follows:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;int v[12]={0,1,5,10,20,50,100,200,500,1000,2000};int c[12],sum[12];int p,ans;void dfs(int i,int rest,int count){ if(rest<0) return ; if(i==0) { if(rest==0) ans=max(ans,count); return ; } int tmp=max(0,rest-sum[i-1]); int cn=tmp/v[i]+(tmp%v[i]!=0); if(cn<=c[i]) dfs(i-1,rest-cn*v[i],count+cn); cn++; if(cn<=c[i]) dfs(i-1,rest-cn*v[i],count+cn);}int main(){ ///cout << "Hello world!" << endl; int T; cin>>T; while(T--) { scanf("%d",&p); for(int i=1;i<=10;i++) scanf("%d",&c[i]); sum[0]=0; for(int i=1;i<=10;i++) sum[i]=sum[i-1]+v[i]*c[i]; ans=-1; dfs(10,p,0); printf("%d\n",ans); } return 0;}///1020 0 0 0 49 1 0 0 0 1 0