Language:
Allowance
Description
As a reward for record milk production, Farmer John has decided to start paying Bessie the cow a small weekly allowance. FJ has a set of coins in N (1 <= N <= 20) different denominations, where each denomination of coin evenly divides the next-larger denomination
(e.g., 1 cent coins, 5 cent coins, 10 cent coins, and 50 cent coins).Using the given set of coins, he would like to pay Bessie at least some given amount of money C (1 <= C <= 100,000,000) every week.Please help him ompute the maximum number of weeks he can
pay Bessie.
Input
* Line 1: Two space-separated integers: N and C
* Lines 2..N+1: Each line corresponds to a denomination of coin and contains two integers: the value V (1 <= V <= 100,000,000) of the denomination, and the number of coins B (1 <= B <= 1,000,000) of this denomation in Farmer John‘s possession. Output
* Line 1: A single integer that is the number of weeks Farmer John can pay Bessie at least C allowance
Sample Input 3 6 10 1 1 100 5 120 Sample Output 111 Hint
INPUT DETAILS:
FJ would like to pay Bessie 6 cents per week. He has 100 1-cent coins,120 5-cent coins, and 1 10-cent coin. OUTPUT DETAILS: FJ can overpay Bessie with the one 10-cent coin for 1 week, then pay Bessie two 5-cent coins for 10 weeks and then pay Bessie one 1-cent coin and one 5-cent coin for 100 weeks. Source |
思路:可以贪心(因为面值之间是相互整除的),面值比c大的一天发一张,比c小的就优先选面值大的,从大到小贪一遍,这个过程中要保证所选硬币面值之和m要小于等于c,若m小于c,则从小往大优先选一张能使m大于等于c,这样是最优的。(开始没用need,1000ms险过。。。后来看到网上用的need,0ms。。。太菜)
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <string> #include <map> #include <stack> #include <vector> #include <set> #include <queue> #pragma comment (linker,"/STACK:102400000,102400000") #define maxn 1005 #define MAXN 2005 #define mod 1000000009 #define INF 0x3f3f3f3f #define pi acos(-1.0) #define eps 1e-6 #define lson rt<<1,l,mid #define rson rt<<1|1,mid+1,r #define FRE(i,a,b) for(i = a; i <= b; i++) #define FRL(i,a,b) for(i = a; i < b; i++) #define mem(t, v) memset ((t) , v, sizeof(t)) #define sf(n) scanf("%d", &n) #define sff(a,b) scanf("%d %d", &a, &b) #define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c) #define pf printf #define DBG pf("Hi\n") typedef long long ll; using namespace std; struct Money { int v,b; }cent[21]; int n,c; int need[21]; int cmp(Money x,Money y) { return x.v<y.v; } int main() { int i,j; while (~sff(n,c)) { FRL(i,0,n) sff(cent[i].v,cent[i].b); sort(cent,cent+n,cmp); int r=n-1; int ans=0; while (cent[r].v>=c){ //比c大的直接加上数量,一天发一张 ans+=cent[r].b; cent[r].b=0; r--; } while (1) { mem(need,0); int mm=c; for (i=r;i>=0;i--) //从大到小 { int num=min(cent[i].b , mm/cent[i].v); mm-=num*cent[i].v; need[i]=num; } if (mm>0) //没凑够的从小往大的找一张能凑够的最小的面值 { FRL(i,0,n) { if (cent[i].b>0&&mm-cent[i].v<=0){ need[i]++; mm-=cent[i].v; break; } } } if (mm>0) break; int minn=INF; FRL(i,0,n) if (need[i]) minn=min(minn,cent[i].b/need[i]); ans+=minn; FRL(i,0,n) if (need[i]) cent[i].b-=minn*need[i]; } printf("%d\n",ans); } return 0; }
原文地址:http://blog.csdn.net/u014422052/article/details/44114827