zoj 2109 FatMouse' Trade 贪心

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
struct data
{
    double j,f,x;
}a[1024];
bool cmp(data a,data b)
{
    return a.x>b.x;
}

int main()
{
    int m,n,i;
    double sum;
    while(~scanf("%d %d",&m,&n))
    {
        if(m==-1&&n==-1) break;
        for(i=0;i<n;i++)
        {
            scanf("%lf%lf",&a[i].j,&a[i].f);
            a[i].x=a[i].j/a[i].f;
        }
        sum=0;
        sort(a,a+n,cmp);
        for(i=0;i<n;i++)
        {
            if(m>a[i].f)
            {
                sum+=a[i].j;
                m-=a[i].f;
            }
            else
            {
                sum+=m*a[i].x;
                m=0;
                break;
            }
        }
        printf("%.3f\n",sum);
    }
    return 0;
}



你可能感兴趣的:(zoj 2109 FatMouse' Trade 贪心)