您的位置:首页 > 其它

hdu 1009 FatMouse' Trade (水题,贪心)

2014-07-22 18:09 441 查看
小记:这题比较水

思路:将J[i]/F[i]的值进行从大到小排序,然后依次贪心

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>

using namespace std;

#define mst(a,b) memset(a,b,sizeof(a))
#define REP(a,b,c) for(int a = b; a < c; ++a)
#define eps 10e-8

const int MAX_ = 1010;
const int N = 100010;
const int INF = 0x7fffffff;

struct node{
int s, e;
double dis;
}t[MAX_];

bool cmp(const node& a, const node& b)
{
if(a.dis < b.dis)return 0;
return 1;
}

int main()
{
int T;
int n, m;
while(scanf("%d%d",&n, &m)) {
if(n == -1 && m == -1)break;

REP(i, 0, m) {
scanf("%d%d", &t[i].s, &t[i].e);
t[i].dis = t[i].s*1.0/t[i].e;

}
sort(t, t+m, cmp);

double ans = 0;

REP(i, 0, m){
if(t[i].e >= n){
ans += t[i].dis * n;
break;
}
else {
ans += t[i].s;
n -= t[i].e;
}
}
printf("%.3f\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: