您的位置:首页 > 其它

HDU1009(贪心)

2015-10-30 01:16 302 查看
水题~

按照性价比排序。

#include <bits/stdc++.h>
using namespace std;
#define maxn 1111

int n, m;
struct node {
    double val;
    int cnt;
    bool operator < (const node &a) const {
        return val/cnt > a.val/a.cnt;
    }
}a[maxn];
int tot;

int main () {
    //freopen ("in", "r", stdin);
    while (scanf ("%d%d", &m, &n) == 2) {
        if (n == -1 && m == -1)
            break;
        int sum = 0;
        tot = 0;
        for (int i = 0; i < n; i++) {
            scanf ("%lf%d", &a[i].val, &a[i].cnt);
        }
        sort (a, a+n);
        double ans = 0;
        for (int i = 0; i < n; i++) { 
            if (sum+a[i].cnt > m) {
                ans += a[i].val*(m-sum)/a[i].cnt;
                break;
            }
            else {
                ans += a[i].val;
                sum += a[i].cnt;
            }
        }
        printf ("%.3f\n", ans);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: