hdu - 4442 - Physical Examination

题意:WANGPENG排队体检,共n个项目,对于第i个项目,完成该项目要ai的时间,若不在队列中,每过1秒需要等bi秒,问他完成所有项目的最短时间。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4442

——>>看了题后,我想到了贪心,但是怎样贪法呢?看看题中的数据,想了下,是完成时间少的排在前面,完成时间相等时等待时间长的排在前面,于是写了个,submit——WA了!原来贪心是推出来的,刚才随便想的真错!:

        假设有2个项目(a1, b1), (a2, b2),

先排第一个时,总时间为:a1 + a1*b2 + a2 —— a1 + a2 + a1*b2;

先排第二个时,总时间为:a2 + a2*b1 + a1 —— a1 + a2 + a2*b1;

对比可发现,排序的依据是:a1*b2与a2*b1的大小。

另外发现:题中说:0≤ai,bi<231但是开int来存ai与bi却会WA,需用64位整数来存;还有那个取模的,需要每步取模,若只是最后取一次模,结果还是WA!这题,真是……

#include <cstdio>
#include <algorithm>

using namespace std;

const int MOD = 365*24*60*60;
const int maxn = 100000 + 10;
struct node
{
    long long a;
    long long b;
    bool operator < (const node &e) const
    {
        return a*e.b < e.a*b;
    }
}item[maxn];

int main()
{
    int n, i;
    while(~scanf("%d", &n))
    {
        if(!n) return 0;
        for(i = 0; i < n; i++) scanf("%I64d%I64d", &item[i].a, &item[i].b);
        sort(item, item+n);
        long long sum = 0;
        for(i = 0; i < n; i++) sum = (sum + sum*item[i].b + item[i].a) % MOD;       //每步取模,若只是最后取模WA!
        printf("%I64d\n", sum);
    }
    return 0;
}



你可能感兴趣的:(hdu - 4442 - Physical Examination)