FatMouse' Trade
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 21 Accepted Submission(s) : 13
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
FatMouse prepared M pounds of cat food, ready to trade with the cats guarding the warehouse containing his favorite food, JavaBean. The warehouse has N rooms. The i-th room contains J[i] pounds of JavaBeans and requires F[i] pounds of cat food. FatMouse does not have to trade for all the JavaBeans in the room, instead, he may get J[i]* a% pounds of JavaBeans if he pays F[i]* a% pounds of cat food. Here a is a real number. Now he is assigning this homework to you: tell him the maximum amount of JavaBeans he can obtain.
Input
The input consists of multiple test cases. Each test case begins with a line containing two non-negative integers M and N. Then N lines follow, each contains two non-negative integers J[i] and F[i] respectively. The last test case is followed by two -1's. All integers are not greater than 1000.
Output
For each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum amount of JavaBeans that FatMouse can obtain.
Sample Input
5 3 7 2 4 3 5 2 20 3 25 18 24 15 15 10 -1 -1
Sample Output
13.333 31.500
Author
Source
ZJCPC2004
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main() 6 { 7 int M,N,i,J[10000],F[10000],k,j,tmp,sign; 8 double SUM; 9 while(scanf("%d%d",&M,&N)!=EOF) 10 { 11 if(M==-1&&N==-1) 12 break; 13 k=0;SUM=0; 14 while(N--) 15 { 16 scanf("%d%d",&J[k],&F[k]); 17 k++; 18 } 19 for(i=k-1;i>=0;i--) 20 { 21 sign=i; 22 for(j=i-1;j>=0;j--) 23 if(J[sign]*F[j]>J[j]*F[sign])sign=j; 24 if(sign!=i) 25 { 26 tmp=J[i]; 27 J[i]=J[sign]; 28 J[sign]=tmp; 29 30 tmp=F[i]; 31 F[i]=F[sign]; 32 F[sign]=tmp; 33 } 34 } 35 36 for(i=0;i<k;i++) 37 { 38 M-=F[i]; 39 if(M<0) 40 {SUM+=((J[i]*1.0)/F[i])*(M+F[i]);break;} 41 SUM+=J[i]; 42 } 43 printf("%.3lf ",SUM); 44 45 } 46 return 0; 47 }