贪心_简单直接贪心[优先队列](HDU_1009)

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;

#define M 1002

struct node {
    int j,f;
    node(int _j,int _f)
    {
        j = _j; f = _f;
    }
    friend bool operator < (const node a,const node b)
    {
        return 1.0 * a.j / a.f < 1.0 * b.j / b.f;
    }
};

priority_queue<node> q;

int main(int argc, char* argv[])
{
    #ifdef __MYLOCAL
    freopen("in.txt","r",stdin);
    #endif

    int m,n,Ji,Fi;
    double s;
    while(scanf("%d%d",&m,&n) + m + n)
    {
        while(!q.empty())
        {
            q.pop();
        }
        while(n--)
        {
            scanf("%d%d",&Ji,&Fi);
            q.push(node(Ji,Fi));
        }
        s = 0;
        while(m > 0 && !q.empty())
        {
            if(m >= q.top().f)
                s += q.top().j;
            else
                s += 1.0 * m / q.top().f * q.top().j;
            m -= q.top().f;
            q.pop();
        }
        printf("%.3lf\n",s);
    }

    return 0;
}

 

posted on 2013-08-15 09:57  lk1993  阅读(161)  评论(0编辑  收藏  举报