这题真不应该花了那么多时间。当时有了一个想法,但是我没有顺着这个想法做下去。后来还是zz_1215那样做了,果断AC。他说:“我不知道为什么这样,但哥就是AC啦!”
其实是这样的:
假设已经花了t秒,而现在有两个项目可选,所需时间分别为a1, b1和a2, b2。
先选项目1再选项目2所需时间为:t1 = a1+b1*t + b2*(a1+b1*t+t)+a2 = a1+a2+(b1+b2)*t+b1*b2*t+a1*b2;
先选项目2再选项目1所需时间为:t2 = a2+b2*t + b1*(a2+b2*t+t)+a1 = a1+a2+(b1+b2)*t+b1*b2*t+a2*b1.
可以发现,两种选法对结果的影响的差别只与以上两式的最后一项有关,由此就可以确定排序方法。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
const int maxn = 100010;
const int mod = 365*24*60*60;
int n;
struct Node {
long long a, b;
}e[maxn];
bool cmp(Node n1, Node n2)
{
return n1.a * n2.b < n1.b * n2.a;
}
int main()
{
while (scanf("%d", &n) != EOF) {
if (n == 0) break;
for (int i = 0; i < n; ++i) {
scanf("%lld%lld", &e[i].a, &e[i].b);
}
sort(e, e + n, cmp);
long long ans = 0;
for (int i = 0; i < n; ++i) {
ans += e[i].a + ans * e[i].b;
ans %= mod;
}
printf("%lld\n", ans);
}
return 0;
}