Exact Change(背包HDU2753)
2015-08-24 17:11
106 查看
Exact Change
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 545 Accepted Submission(s): 172
Problem Description
* Seller: That will be fourteen dollars.
* Buyer: Here’s a twenty.
* Seller: Sorry, I don’t have any change.
* Buyer: OK, here’s a ten and a five. Keep the change.
When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change.
Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.
Input
The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., 100).Thefollowinglinecontainsasingleintegern,thenumberofbillsandcoinsthatyouhave.Thenumbernisatmost100.Thefollowingnlineseachcontainoneinteger,thevalueincentsofeachbillorcointhatyouhave.Notethatthedenominationscanbeanynumberofcents;theyarenotlimitedtothevaluesofcoinsandbillsthatweusuallyuseinCanada.However,nobillorcoinwillhaveavaluegreaterthan10000cents(100). The total value of your bills and coins will always be equal to or greater than the price of the item.
Please process to the end of the data file.
Output
For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.
Sample Input
1
1400
3
500
1000
2000
1
1400
3
500
1000
2000
Sample Output
1500 2
1500 2
Source
University of Waterloo Local Contest 2008.10.04
题意:买一件价值为m的物品,在对方不找零的情况下,拿出最少的前n(n>=m),钱数相同的情况下,钱的数量最少.
思路:
将自己的金钱进行01背包,然后查找最接近m的钱
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 545 Accepted Submission(s): 172
Problem Description
* Seller: That will be fourteen dollars.
* Buyer: Here’s a twenty.
* Seller: Sorry, I don’t have any change.
* Buyer: OK, here’s a ten and a five. Keep the change.
When travelling to remote locations, it is often helpful to bring cash, in case you want to buy something from someone who does not accept credit or debit cards. It is also helpful to bring a variety of denominations in case the seller does not have change. Even so, you may not have the exact amount, and will have to pay a little bit more than full price. The same problem can arise even in urban locations, for example with vending machines that do not return change.
Of course, you would like to minimize the amount you pay (though you must pay at least as much as the value of the item). Moreover, while paying the minimum amount, you would like to minimize the number of coins or bills that you pay out.
Input
The first line of each test chunk contains an integer specifying the number of test cases in this chunk to follow. Each test case begins with a line containing an integer, the price in cents of the item you would like to buy. The price will not exceed 10 000 cents (i.e., 100).Thefollowinglinecontainsasingleintegern,thenumberofbillsandcoinsthatyouhave.Thenumbernisatmost100.Thefollowingnlineseachcontainoneinteger,thevalueincentsofeachbillorcointhatyouhave.Notethatthedenominationscanbeanynumberofcents;theyarenotlimitedtothevaluesofcoinsandbillsthatweusuallyuseinCanada.However,nobillorcoinwillhaveavaluegreaterthan10000cents(100). The total value of your bills and coins will always be equal to or greater than the price of the item.
Please process to the end of the data file.
Output
For each test case, output a single line containing two integers: the total amount paid (in cents), and the total number of coins and bills used.
Sample Input
1
1400
3
500
1000
2000
1
1400
3
500
1000
2000
Sample Output
1500 2
1500 2
Source
University of Waterloo Local Contest 2008.10.04
题意:买一件价值为m的物品,在对方不找零的情况下,拿出最少的前n(n>=m),钱数相同的情况下,钱的数量最少.
思路:
将自己的金钱进行01背包,然后查找最接近m的钱
#include <set> #include <map> #include <list> #include <stack> #include <cmath> #include <vector> #include <queue> #include <string> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int INF = 0x3f3f3f3f; const double eps = 1e-5; int n,m; int sum; int Dp[110000]; int a[110]; int main() { int T; while(~scanf("%d",&T)) { while(T--) { scanf("%d %d",&m,&n); sum=0; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum=max(sum,a[i]); } sum+=m; memset(Dp,INF,sizeof(Dp)); Dp[0]=0; for(int i=1;i<=n;i++) { for(int j=sum;j>=a[i];j--) { if(Dp[j-a[i]]!=INF)//标记,判断这个状态是否存在 { Dp[j]=min(Dp[j],Dp[j-a[i]]+1); } } } for(int i=m;;i++)//查找 { if(Dp[i]!=INF) { printf("%d %d\n",i,Dp[i]); break; } } } } return 0; }
相关文章推荐
- hdu 5407CRB and Candies
- hdoj 1853 Cyclic Tour 【最小费用最大流 or KM算法】【构图后可以判断图中是否存在哈密顿环】
- 程序员的年龄天花板
- 深入了解Unicode和UTF-8
- CSU_1507_超大型LED显示屏
- 排序算法(六)归并排序
- sql语句经典
- 【转】ARM虚拟化调研
- Attribute "resultCalss" must be declared for element type "select".
- 怎么将Android的API demo导入到Eclipse工作区中
- cassandra安装
- poj2528 Mayor's posters(线段树区间覆盖)
- Exact Change(背包HDU2753)
- Spring MVC ControllerClassNameHandlerMapping example
- Ubuntu logomaker sh: 1: pngtopnm: not found 解决方案
- 人生苦短,我学python -- 入门篇
- listview、gradview滚动到最后时,滑动至顶部
- 斐波那契序列
- STL学习----入门(1)[array]
- listview、gradview滚动到最后时,滑动至顶部