1. 程式人生 > >HDU 3348 coins 貪心 最少紙幣和最多紙幣

HDU 3348 coins 貪心 最少紙幣和最多紙幣

[題目連結](http://acm.hdu.edu.cn/showproblem.php?pid=3348)                  

coins

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2074 Accepted Submission(s): 669

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

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.

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

這道題本來很簡單,求最多紙幣如果直接求那是比較費勁的,如果反過來想問題的話那就簡單很多了,具體看程式碼


#include <algorithm>
#include <string.h>
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <queue> #include <map> #include <set> #include<cmath> #include <iomanip> using namespace std; const int N = 1e6; const double esp=1e-5; int money[]= {1,5,10,50,100}; int Count[5]; int t,p; int SolveMin(int p) { int ans=0,m; for(int i=4; i>=0; i--) { m=min(p/money[i],Count[i]); p-=m*money[i]; ans+=m; } if(p==0) return ans; return -1; } int main() { cin>>t; while(t--) { cin>>p; int sum=0,sumCout=0; for(int i=0; i<=4; i++) { cin>>Count[i]; sum+=Count[i]*money[i]; sumCout+=Count[i]; } if(sum<p) printf("-1 -1\n"); else { int s=SolveMin(p); cout<<s<<" "<<sumCout-SolveMin((sum-p))<<endl; } } return 0; }