题意:你有1 5 10 50 ·100 (jiao)的纸币数量分别为a1 a2 a3 a4 a5
要去买一本价值为 P 的书,要求不用找钱(也就是你出的钱价值量正好是P)问使用最小的纸币数量,最多的纸币数量。
思路:一看就是贪心,求最小数量简单,但是求最大数量让我想了很久,第一次想要先最小化数量求出所应用的钱,然后对这些钱(用以小换大的方式去最大化数量),可是一直wrong。应该是这个贪心策略一直就不对。。。。逆向思维啊,就对了,想要求P价值对大数量的钱,也就是求(allValue - p)价值最小数量的钱。写一个Function就OK了。、
以下是代码!
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;
#define LOCAL
#define INF 0x3f3f3f3f
#define MAX_N 10000= 1;
int value[] = {1, 5, 10, 50, 100};
int amount[10];
int solveMin(int pt)
{
int ans = 0;
for(int i = 4; i >= 0; i--) {
int amt = min(pt / value[i], amount[i]);
pt -= value[i] * amt;
ans += amt;
}
if(pt == 0)
return ans;
else
return -1;
}
int main()
{
#ifdef LOCAL
freopen("b:\\data.in.txt", "r", stdin);
#endif
int T;
scanf("%d", &T);
while(T--) {
int p;
scanf("%d", &p);
int allvalue = 0;
int allamount = 0;
for(int i = 0; i < 5; i++) {
scanf("%d", &amount[i]);
allvalue += amount[i] * value[i];
allamount += amount[i];
}
int mi = solveMin(p);
if(mi == -1)
printf("-1 -1\n");
else
{
int remain = allvalue - p;
int ma = allamount - solveMin(remain);
printf("%d %d\n", mi, ma);
}
}
return 0;
}
Description
"Yakexi, this is the best age!" Dong MW works hard and get high pay, he has many 1 Jiao and 5 Jiao banknotes(纸币), some day he went to a bank and changes part of his money into 1 Yuan, 5 Yuan, 10 Yuan.(1 Yuan = 10 Jiao)
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn't like to get the change, that is, he will give the bookseller exactly P Jiao.
"Thanks to the best age, I can buy many things!" Now Dong MW has a book to buy, it costs P Jiao. He wonders how many banknotes at least,and how many banknotes at most he can use to buy this nice book. Dong MW is a bit strange, he doesn't like to get the change, that is, he will give the bookseller exactly P Jiao.
Input
T(T<=100) in the first line, indicating the case number.
T lines with 6 integers each:
P a1 a5 a10 a50 a100
ai means number of i-Jiao banknotes.
All integers are smaller than 1000000.
T lines with 6 integers each:
P a1 a5 a10 a50 a100
ai means number of i-Jiao banknotes.
All integers are smaller than 1000000.
Output
Two integers A,B for each case, A is the fewest number of banknotes to buy the book exactly, and B is the largest number to buy exactly.If Dong MW can't buy the book with no change, output "-1 -1".
Sample Input
3 33 6 6 6 6 6 10 10 10 10 10 10 11 0 1 20 20 20
Sample Output
6 9 1 10 -1 -1