【hdu 5527】 [2015ACM/ICPC亚洲区长春站] Too Rich 贪心
2016-04-21 08:23
513 查看
Too Rich
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 673 Accepted Submission(s): 173
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 p dollars 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 10coins,four10 coins, four 5 coins, and eight 1coins,youwillpayitbytwo1 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.
1≤T≤20000
0≤p≤109
0≤ci≤100000
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
3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0
Sample Output
9
-1
36
Source
2015ACM/ICPC亚洲区长春站
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5527
题意:
选出凑成N的最多的钱数;
思路:
从大面值到小面值贪心,剩下的只要可以被取到便取最少的大面值的;因为500,200等非倍数的存在,
反例:150,用50,20取,故取时500,50,等可能多取一个即可;
代码:
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 673 Accepted Submission(s): 173
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 p dollars 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 10coins,four10 coins, four 5 coins, and eight 1coins,youwillpayitbytwo1 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.
1≤T≤20000
0≤p≤109
0≤ci≤100000
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
3
17 8 4 2 0 0 0 0 0 0 0
100 99 0 0 0 0 0 0 0 0 0
2015 9 8 7 6 5 4 3 2 1 0
Sample Output
9
-1
36
Source
2015ACM/ICPC亚洲区长春站
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=5527
题意:
选出凑成N的最多的钱数;
思路:
从大面值到小面值贪心,剩下的只要可以被取到便取最少的大面值的;因为500,200等非倍数的存在,
反例:150,用50,20取,故取时500,50,等可能多取一个即可;
代码:
#include<iostream> #include<stdio.h> using namespace std; int v[11]={0,1,5,10,20,50,100,200,500,1000,2000}; int s[11]; int ans;int N; long long T[11]; void dfs(int p,int n,int d) { if(p<0) return ; if(d==0) { if(!p) ans=max(ans,n); return ; } long long tmp=max((long long)0,p-T[d-1]); int k=tmp/v[d]+(tmp%v[d]?1:0); if(k>s[d]) return ; dfs(p-k*v[d],k+n,d-1); k++; if(k<=s[d]) dfs(p-k*v[d],k+n,d-1); } int main() { int c; scanf("%d",&c); while(c--) { scanf("%d",&N); for(int i=1;i<=10;i++) scanf("%d",&s[i]); T[0]=0; for(int i=1;i<=10;i++) { T[i]=T[i-1]+(long long)s[i]*(long long)v[i]; } ans=0; dfs(N,0,10); if(!ans) printf("-1\n"); else printf("%d\n",ans); } }
相关文章推荐
- c++作业4
- ifram 取父窗体的URL地址
- cacheHelper
- 第四周项目三猜数字游戏
- 14级商软2班软件工程大作业分组定题
- 第七周项目2—友元类
- CSS position属性和实例应用
- bzoj3745: [Coci2015]Norma
- 拿什么拯救你,我的代码--c#编码规范实战篇
- 论项目的UI设计
- CustomerConfigHelper
- 第七周项目1—两点间的距离—成员函数、友元函数和一般函数
- MyBatis中SQL映射的XML文件
- 第五周项目一—三角形类定义成员函数
- LeetCode 172. Factorial Trailing Zeroes
- 三角形5
- 响应式布局图片处理
- LeetCode 311. Sparse Matrix Multiplication(稀疏矩阵相乘)
- 详解css3弹性盒模型(Flexbox)
- Outlook使用技巧