http://acm.hdu.edu.cn/showproblem.php?pid=1009
典型的贪心问题,很简单咯。
//贪心
#include <iostream>
using namespace std;
#include <cstdio>
#include <algorithm>
#define MAX 1001
typedef struct _node
{
int j;
int f;
double per;
}node;
node no[MAX];
bool cmp(node a, node b)
{
return a.per > b.per;
}
int main()
{
int n, m, j, f, i;
double sum;
while(scanf("%d%d", &n, &m)&&n!=-1&&m!=-1){
sum = 0.0;
for(i = 0; i < m; i ++){
scanf("%d%d", &j, &f);
no[i].j = j, no[i].f = f, no[i].per = j*1.0/f;
}
sort(no, no+m, cmp);
for(i = 0; i < m; i ++){
if(n - no[i].f >= 0){
sum += no[i].j;
n -= no[i].f;
}
else {
sum += n*no[i].per;
n = 0;
break;
}
}
printf("%.3lf\n", sum);
}
return 0;
}