hdu 1009 FatMouse' Trade (水題,貪心)

小記:這題比較水


思路:將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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章