HDU 1009FatMouse ' Trade

Source: Internet
Author: User
Fatmouse ' trade Time limit:2000/1000ms (java/other) Memory limit:65536/32768k (java/other) Total Submission (s): Accepted submission (s): font:times New Roman | Verdana | Georgia Font size:←→ 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 amount of J Avabeans that Fatmouse can obtain.
Sample Input
5 3
7 2
4 3
5 2
-3
-1-1
Sample Output
13.333
31.500
AuthorCHEN, Yue Source ZJCPC2004 Statistic | Submit | Back to the main idea: rats have m-pound cat food, in a place there are n rooms, each here has a cat in the guard, the first room of j[i] pound beans required f[i] pounds cat food room beans can all take away, but the mouse does not have to take all, if the mouse gave the cat f[i]*a% pounds cat food, then he can get j[ i]*a% pounds of beans, the maximum number of beans that can be obtained by the mouse is how much. Idea: The question is the equivalent of buying something, how to spend the least amount of money to buy something that may be more recent. So that's the money (cat food) to buy at least which one, that is, the same amount of money (cat food), which buy more first buy which. That is, first buy J[i]/f[i] The biggest one, if not cat food requirements, then buy m/f[i]*j[i] beans. You can first sort the j[i]/f[i] and then buy the code from the top down:
#include <iostream> #include <iomanip> #define MAX 9999999 using namespace std;
Double j[1000],f[1000],p[1000];
    void order (double p[],double j[],double f[],int n) {int i,s,k;
        for (i=n; i>0; i--) {k=0;
            for (s=0; s<i; s++) {if (P[k]>=p[s]) {k=s;
        }} double T;
        T=P[K];
        P[K]=P[I-1];
        p[i-1]=t;
        T=J[K];
        J[K]=J[I-1];
        j[i-1]=t;
        T=F[K];
        F[K]=F[I-1];
    f[i-1]=t;
    }} int main () {int n,i; double s,m;
        while (cin>>m>>n&& (n!=-1&&m!=-1)) {s=0;
            for (i=0; i<n; i++) {cin>>j[i]>>f[i];
            if (f[i]!=0)//denominator equals zero case processing p[i]=j[i]/f[i];
            Else P[i]=max;//max assumes infinite} order (p,j,f,n);//Sort for (i=0; i<n; i++) {
         if (M>=f[i])//enough to pay   {S=s+j[i];
            M=m-f[i];
                } else if (M>=0&&m<f[i]) cannot be paid {s=s+m*p[i];
            m=0;
        }} cout<<setiosflags (ios::fixed) <<setprecision (3);
    cout<<s<<endl;
} return 0;
 }


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.