C - FatMouse' Trade

FatMouse' Trade

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 45965    Accepted Submission(s): 15395


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
 
 
首先这是一道贪心算法,当f/j越大时代表这样越有利,所以进行排序;
但也有值得注意的几组数据(来自杭电的大神)
此题除了要满足例子以外,还要满足一些条件才能真正算ac:
0 1
1 0
1.000

1 0
0.000

5 4
10000 5
2000 2
100 0
300 0
10400.000

数据类型用double,就这样
 1 #include<stdio.h>
 2 int j[1010],f[1010];
 3 double a[1010];
 4 void paixu(double a[],int k[],int f[],int n)
 5 {
 6     int i=0,j=n-1,h=k[0],m=f[0];
 7     double  t=a[0];
 8     if(n>1){
 9         while(i<j)
10         {
11             for(;i<j;j--)
12             if(a[j]>t){
13                 a[i]=a[j];
14                 f[i]=f[j];
15                 k[i]=k[j];
16                 i++;
17                 break;
18             }
19             for(;i<j;i++)
20             {
21                 if(a[i]<t){
22                     a[j]=a[i];
23                     f[j]=f[i];
24                     k[j]=k[i];
25                     j--;
26                     break;
27                 }
28             }
29         }
30         a[i]=t;
31         f[i]=m;
32         k[i]=h;
33         paixu(a,k,f,i);
34         paixu(a+i+1,k+i+1,f+i+1,n-i-1);
35     }
36 }
37 void hanshu(int m,int n)
38 {
39     int i;
40     double  sum=0;
41     for(i=0;i<n;i++)
42     {
43         if(m>=f[i]){
44             sum=sum+j[i];
45             m=m-f[i];
46         }
47         else{
48             sum=sum+1.0*m/f[i]*j[i];
49             break;
50         }
51     }
52     printf("%0.3f\n",sum);
53 }
54 int main()
55 {
56     int n,m,i,t;
57     while(1)
58     {scanf("%d%d",&m,&n);
59     if(m==-1&&n==-1)break;
60     for(i=0;i<n;i++)
61         {scanf("%d%d",&j[i],&f[i]);
62         if(f[i]!=0)a[i]=1.0*j[i]/f[i];
63         else a[i]=1020;
64         }
65         paixu(a,j,f,n);
66         hanshu(m,n);
67         }
68     return 0;
69 
70 }

 

 

posted @ 2014-12-16 19:53  dongq  阅读(348)  评论(0编辑  收藏  举报