HDU1009 贪心水题

题目:HDU1009

每个房间里的J除以F得到比例,按照比例排序,拿食物去比例较大的房间换

#include <iostream>
#include <algorithm>
//#include <fstream>
#include <iomanip>
#define FOR(a,b) for(int i = (a);i < (b);i ++)
using namespace std;

struct Node
{
    double j,f;
    double proportion;
    bool operator<(Node& n)
    {
        return proportion > n.proportion;
    }
}N[1008];



int main()
{
    //ifstream cin("in.txt");
    int n;
    double m;
    double ans;
    while(cin>>m>>n,m != -1)
    {
        memset(N,0,sizeof(Node)*n);
        ans = 0;
        FOR(0,n)
        {
            cin>>N[i].j>>N[i].f;
            N[i].proportion = N[i].j / N[i].f;
        }
        sort(N,N+n);
        FOR(0,n)
        {
            if(N[i].f <= m)
            {
                m -= N[i].f;
                ans += N[i].j;
            }else if(m <= 0){
                break;
            }else{
                ans += m * N[i].proportion;
                break;
            }
        }
        cout<<fixed<<setprecision(3)<<ans<<endl;
    }
}
posted @ 2012-09-28 00:19  iFinVer  阅读(227)  评论(0编辑  收藏  举报