码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 3979【贪心】

时间:2018-04-21 17:37:52      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:ceil   math   mes   names   stream   10000+   return   cas   最小   

这题刚开始想当然的直接按g值排序了。
正确做法是,由于要失去的血量最小,则若此时有两个monster A,B
先A后B失去的血量为 (timeA+timeB)gB+timeAgA
先B后A失去的血量为(timeA+timeB)gA+timeBgB
按这个排序即可
!注意这里有个精度问题,long long才能过

#include <iostream>
#include <queue>
#include <algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define MAX 10000+10
int n, m;
struct M {
    int hp, g;
    double t;
}monster[MAX];

int cmp(const M&a, const M&b) {
    return  a.t*b.g < b.t*a.g;
}

int main(void) {
    int t;
    int Kase = 0;
    scanf("%d", &t);
    int Time;

    while (t--) {
        long long ans = 0;
        Time = 0;
        scanf("%d%d", &n, &m);
        int hp, g;
        for (int i = 1; i <= n; i++) {
            scanf("%d%d", &hp, &g);
            monster[i].hp = hp;
            monster[i].g = g;
            monster[i].t = (__int64)ceil((double)monster[i].hp / (double)m);
        }
        sort(monster + 1, monster + 1 + n, cmp);
        for (int i = 1; i <= n; i++) {
            Time += monster[i].t;
            ans += (long long)monster[i].g*Time;

        }
        printf("Case #%d: %I64d\n", ++Kase, ans);
    }

    return 0;

}

HDU 3979【贪心】

标签:ceil   math   mes   names   stream   10000+   return   cas   最小   

原文地址:https://www.cnblogs.com/tennant/p/8901838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!