首页 > ACM题库 > HDU-杭电 > HDU 2753-HOJ-Exact Change-背包问题-[解题报告]C++
2014
02-14

HDU 2753-HOJ-Exact Change-背包问题-[解题报告]C++

Exact Change

问题描述 :

* 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.

输入:

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). The following line contains a single integer n, the number of bills and coins that you have. The number n is at most 100. The following n lines each contain one integer, the value in cents of each bill or coin that you have. Note that the denominations can be any number of cents; they are not limited to the values of coins and bills that we usually use in Canada. However, no bill or coin will have a value greater than 10 000 cents ($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.

输出:

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). The following line contains a single integer n, the number of bills and coins that you have. The number n is at most 100. The following n lines each contain one integer, the value in cents of each bill or coin that you have. Note that the denominations can be any number of cents; they are not limited to the values of coins and bills that we usually use in Canada. However, no bill or coin will have a value greater than 10 000 cents ($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.

样例输入:

1
1400
3
500
1000
2000
1
1400
3
500
1000
2000

样例输出:

1500 2
1500 2


一道很简单的0-1背包;


不过条件是每个只能用一次(不是完全背包);因为这个理解的错误,怎么也是wrong anwer,


后来用0-1一试,a掉了.不过发现速度差距很大,要来傻崽的代码,发现题目有两个条件可以挖掘,就是每个数据都不打于10000,所以背包最大放到20001就够了.果然加速不少,从原来的238ms到64ms,后来发现还可以加速,就是写法换了一下.加到了15ms,爽啊…

解题参考:http://yxq0620.blog.163.com/blog/static/4449439220102812734832/