题意:要参加一场考试,有n个科目,每个科目就是一个考试队列,每个科目队列有如下两个属性,a,b,那么该科目的消耗时间=a+b*(不在
该队列排队的时间);问给出n个科目队列,求考完所有科目总耗时最少是多少。
思路:假设有x y两个科目,怎样安排先后顺序呢,假设x在前, 时间为ax+ay+ax*by,假设y在前, 时间为ay+ax+ay*bx, 要让x在前的条件是
ax+ay+ax*by < ay+ax+ay*bx, 即ax*by < ay*bx,按这个将所有队列进行排序即可。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int mod = 365*24*60*60;
const int maxn = 1e5+5;
struct node
{
ll a, b;
bool operator < (const node &x) const
{
return a*x.b < b*x.a;
}
}a[maxn];
int main(void)
{
int n;
while(cin >> n && n)
{
for(int i = 1; i <= n; i++)
scanf("%lld%lld", &a[i].a, &a[i].b);
sort(a+1, a+1+n);
ll ans = 0;
for(int i = 1; i <= n; i++)
ans = (ans+a[i].a+ans*a[i].b)%mod;
printf("%lld\n", ans);
}
return 0;
}