HDU 4442 Physical Examination(贪心)

HDU 4442 Physical Examination(贪心)

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

Description

WANGPENG is a freshman. He is requested to have a physical examination when entering the university.
Now WANGPENG arrives at the hospital. Er….. There are so many students, and the number is increasing!
There are many examination subjects to do, and there is a queue for every subject. The queues are getting longer as time goes by. Choosing the queue to stand is always a problem. Please help WANGPENG to determine an exam sequence, so that he can finish all the physical examination subjects as early as possible.

Input

There are several test cases. Each test case starts with a positive integer n in a line, meaning the number of subjects(queues).
Then n lines follow. The i-th line has a pair of integers (ai, bi) to describe the i-th queue:

  1. If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject.
  2. As the queue is getting longer, the waiting time will increase bi seconds every second while WANGPENG is not in the queue.
    The input ends with n = 0.For all test cases, 0<n≤100000, 0≤ai,bi<2^31.

Output

For each test case, output one line with an integer: the earliest time (counted by seconds) that WANGPENG can finish all exam subjects. Since WANGPENG is always confused by years, just print the seconds mod 365×24×60×60.

Sample Input

5
1 2
2 3
3 4
4 5
5 6
0

Sample Output

1419

题意:

给你n个队伍每个队伍都要排一次,每个队伍要排a秒,如果不排,则每秒过后排这个队伍要增加b秒问最少多少时间排完所有的队伍。

题解:

我们现在假设两个队伍分别是a1,b1,a2,b2。现在如果先排第一队,花费a1+a2+a1b1。如果先排第二队则花费a1+a2+a2b1这样我们明显可以得到应该选择a1b2与a2b1中小的那个。这样按照这个排序,得出最小的即可。

代码:

#include <bits/stdc++.h>
using namespace std;
int n;
const int maxn = 100100;
const int mod =  365*24*60*60;
struct node{
    long long a,b;  
    bool operator < (const node &R)const{
        return a*R.b < b*R.a;    
    }
}s[maxn];
int main()
{
    while (scanf("%d",&n)&&(n!=0)){
        for (int i = 0; i < n; i++)
            scanf("%lld %lld",&s[i].a,&s[i].b);
        sort(s,s+n);
        long long ans = 0;
        for (int i = 0; i < n; i++){
            ans = (ans + s[i].a + s[i].b*ans%mod)%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;    
}
posted @ 2016-08-20 12:43  Thecoollight  阅读(175)  评论(0编辑  收藏  举报