Allowance
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 1842 | Accepted: 763 |
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.
* 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
1 #include<algorithm> 2 #include<cstdio> 3 #include<iostream> 4 #include<cstring> 5 using namespace std; 6 struct TT 7 { 8 int v,w; 9 }a[25]; 10 int need[25]; 11 bool cmp(TT m, TT n) 12 { 13 if(m.v>n.v) return true; 14 return false; 15 } 16 int main() 17 { 18 int n,i,k; 19 int value,aa,bb,ans; 20 while(~scanf("%d %d",&n,&value)) 21 { 22 ans = 0; 23 k = 0; 24 for( i=0;i<n;i++) 25 { 26 scanf("%d %d",&aa,&bb);//排除大数; 27 if( aa>= value) 28 { 29 ans = ans+bb; 30 } 31 else 32 { 33 a[k].v = aa; 34 a[k].w = bb; 35 k++; 36 } 37 } 38 //k = n; 39 //printf("sdfgsdf "); 40 sort(a,a+k,cmp); 41 while(1) 42 { 43 memset(need,0,sizeof(need)); 44 int sum = value; 45 for(int i=0; i<k; i++) // Õý×ÅÕÒ 46 { 47 int tmp = sum/a[i].v; 48 need[i] = min(a[i].w,tmp); 49 sum = sum - a[i].v*need[i]; 50 } 51 if(sum>0) 52 { 53 for(int i=k-1;i>=0;i--) 54 { 55 if(a[i].w && a[i].v>=sum) 56 { 57 need[i]++; 58 sum = 0; 59 break; 60 } 61 } 62 } 63 if(sum>0) break; 64 int s = 0x3f3f3f3f; 65 for(int i=0;i<k;i++) 66 { 67 if(need[i]) 68 s = min(s,a[i].w/need[i]); 69 } 70 ans = ans+s; 71 for(int i=0;i<k;i++) 72 { 73 if(need[i]) 74 a[i].w -= need[i]*s; 75 } 76 } 77 printf("%d ",ans); 78 } 79 return 0; 80 }