C-fatmouse ' Trade

Source: Internet
Author: User

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 Descriptionfatmouse prepared M pounds of cat food, ready-to-trade with the cats guarding the warehouse containing He favorite food, JavaBean.
The warehouse has N rooms. The i-th contains j[i] pounds of JavaBeans and requires f[i] pounds of cat food. Fatmouse does not has the to trade for all the JavaBeans in the the the same, instead, he may get j[i]* a% of pounds JavaBeans if he Pays f[i]* a% pounds of cat food. Here A is a real number. Now he's assigning this homework to you:tell him the maximum amount of JavaBeans he can obtain.

Inputthe input consists of multiple test cases. Each test case is begins with a line containing the non-negative integers M and N. Then N lines follow, each contains, non-negative integers j[i] and f[i] respectively. The last test case was followed by Two-1 ' s. All integers is not greater than 1000.

Outputfor each test case, print in a single line a real number accurate up to 3 decimal places, which is the maximum Amoun T of JavaBeans that Fatmouse can obtain.

Sample INPUT5 37 24 35 220 325 1824 1515 10-1-1

Sample Output13.33331.500 First This is a greedy algorithm, when the larger the f/j represents the more advantageous, so to sort; but there are a number of noteworthy groups of data (from Hangzhou power of the Great God)
In addition to satisfying the example, the problem must satisfy some conditions in order to really calculate ac:0 11 01.0001 00.0005 410000 52000 2100 0300 010400.000 data type with double, that's all.
1#include <stdio.h>2 intj[1010],f[1010];3 Doublea[1010];4 voidPaixu (DoubleA[],intK[],intF[],intN)5 {6     intI=0, j=n-1, h=k[0],m=f[0];7     Doublet=a[0];8     if(n>1){9          while(i<j)Ten         { One              for(; i<j;j--) A             if(a[j]>t) { -a[i]=A[j]; -f[i]=F[j]; thek[i]=K[j]; -i++; -                  Break; -             } +              for(; i<j;i++) -             { +                 if(a[i]<t) { Aa[j]=A[i]; atf[j]=F[i]; -k[j]=K[i]; -j--; -                      Break; -                 } -             } in         } -a[i]=T; tof[i]=m; +k[i]=h; - Paixu (a,k,f,i); thePaixu (a+i+1, k+i+1, f+i+1, n-i-1); *     } $ }Panax Notoginseng voidHanshu (intMintN) - { the     inti; +     Doublesum=0; A      for(i=0; i<n;i++) the     { +         if(m>=F[i]) { -sum=sum+J[i]; $m=m-F[i]; $         } -         Else{ -sum=sum+1.0*m/f[i]*J[i]; the              Break; -         }Wuyi     } theprintf"%0.3f\n", sum); - } Wu intMain () - { About     intn,m,i,t; $      while(1) -{scanf ("%d%d",&m,&n); -     if(m==-1&&n==-1) Break; -      for(i=0; i<n;i++) A{scanf ("%d%d",&j[i],&f[i]); +         if(f[i]!=0) a[i]=1.0*j[i]/F[i]; the         Elsea[i]=1020; -         } $ Paixu (a,j,f,n); the Hanshu (m,n); the         } the     return 0; the  -}

C-fatmouse ' Trade

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.