您的位置:首页 > 其它

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

2013-08-15 09:57 393 查看
#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: