hdu5527Too Rich

来源:互联网 发布:电脑看书软件阅读器 编辑:程序博客网 时间:2024/05/01 21:38

Too Rich

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 401    Accepted Submission(s): 119


Problem Description
You 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 many 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.
 

Input
The 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.

1T20000
0p109
0ci100000
 

Output
For 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 Input
317 8 4 2 0 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 Output
9-136
 

Source
2015ACM/ICPC亚洲区长春站-重现赛(感谢东北师大)
 
题目大意是给你一些固定面额的钱的数量以及一个值X,然后让你用最多数量的钱拼出X。
先不问题转换一下,改为拼出(总额-x),如果没有50面额和500面额的话就是贪心了。那就合并两个50面额成为100,合并500成为1000,暴力枚举是否用了50,500
#include<bits/stdc++.h>using namespace std;const int val[]={1,5,10,20,50,100,200,500,1000,2000};int c[11],ans,tot,sum,p;void update(int x,int tmp,int s1,int s2){    if(x<0)return;    for(int i=9;i>=0;i--){        //printf("%d %d %d %d %d %d\n",x,c[i],val[i],tmp,s1,s2);        if(i==4){            if(x>=100*s1){                x-=100*s1;                tmp-=s1*2;            }else{                tmp-=x/100*2;                x%=100;            }            continue;        }        if(i==7){            if(x>=1000*s2){                x-=1000*s2;                tmp-=s2*2;            }else{                tmp-=x/1000*2;                x%=1000;            }            continue;        }        if(x>=c[i]*val[i]){            x-=c[i]*val[i];            tmp-=c[i];        }else{            tmp-=x/val[i];            x%=val[i];        }    }  //  printf("%d %d\n",x,tmp);    if(x==0)ans=max(tmp,ans);}void work(){    ans=-1;tot=sum=0;    scanf("%d",&p);    for(int  i=0;i<10;i++){        scanf("%d",c+i);        tot+=val[i]*c[i];        sum+=c[i];    }    if(tot<p){        printf("-1\n");        return;    }    if(tot==p){        printf("%d\n",sum);        return;    }   // printf("tot =%d  sum= %d p= %d\n",tot,sum,p);    update(tot-p,sum,c[4]/2,c[7]/2);    if(c[4]&&c[7])        update(tot-p-50-500,sum-2,(c[4]-1)/2,(c[7]-1)/2);    if(c[4])        update(tot-p-50,sum-1,(c[4]-1)/2,c[7]/2);    if(c[7])        update(tot-p-500,sum-1,c[4]/2,(c[7]-1)/2);    printf("%d\n",ans);}int main(){    int t;scanf("%d",&t);    while(t--)work();    return 0;}



0 0
原创粉丝点击