HDU 4442 Physical Examination(贪心)

28 篇文章 0 订阅
9 篇文章 0 订阅

Physical Examination

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7669    Accepted Submission(s): 2320


Problem 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≤a i,b i<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
Hint
In the Sample Input, WANGPENG just follow the given order. He spends 1 second in the first queue, 5 seconds in the 2th queue, 27 seconds in the 3th queue, 169 seconds in the 4th queue, and 1217 seconds in the 5th queue. So the total time is 1419s. WANGPENG has computed all possible orders in his 120-core-parallel head, and decided that this is the optimal choice.
 

Source
 

Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:   5960  5959  5958  5957  5956 
 


题目大意:

    有N个队伍,每个队伍开始需要ai秒,每秒需要的时间增加bi秒,求排完所有队伍所需要的最短时间。


解题思路:

    首先非常容易想到O(N^2)的贪心方法,对于每个队伍计算花费=ai+ai*(除它之外的没有排的队伍的时间和),每次选取最小的进行贪心。不过N可以取1e5,这样毫无疑问会T。

    于是在此基础上,我们考虑对于两个连续排的队伍:q1,q2,假设剩下的队伍为:q3。

    则cost(q1)=a[q1]*(1+b[q2]+b[q3]);  cost(q2)=a[q2]*(1+b[q1]+b[q3]);

    把这两个式子拆开:cost(q1)=a[q1]+a[q1]*b[q2]+a[q1]*b[q3]; cost(q2)=a[q2]+a[q2]+a[q2]*b[q1]+a[q2]*b[q3];

    因为这两个队伍是相邻的,所以当我们排完这两个队伍的时候,这两个队伍的花费计算公式的第一项之和一定是定值:a[q1]+a[a2]; 第三项之和也一定是定值:(a[q1]+a[q2])*b[q3]; 所以我们只需要考虑中间一项就可以了。

    于是我们对于两个连续排的队伍,若a[q1]*b[q2]<a[q2]*b[q1]则先排q1,否则先排q2。

    所以就可以把这个式子作为sort()的比较函数,就可以在O(N*log N)的时间完成贪心的排序。

    第一次遇到通过相邻两项比较进行贪心的题目,当时比赛的时候没想到怎么从O(N^2)优化到O(N*log N),还是经验不足啊。


AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
using namespace std;
#define LL long long
#define P pair<LL,LL>
#define fi first
#define se second

const int MAXN=100000+3;
const LL MOD=365*24*60*60;
pair<LL,LL> que[MAXN];
int N;

bool cmp(const P &a,const P &b)
{
    return a.fi*b.se<b.fi*a.se;
}

int main()
{
    while(~scanf("%d",&N)&&N)
    {
        for(int i=0;i<N;++i)
            scanf("%lld%lld",&que[i].fi,&que[i].se);
        sort(que,que+N,cmp);
        LL ans=0;
        for(int i=0;i<N;++i)
            ans=(ans+que[i].fi+que[i].se*ans)%MOD;
        printf("%lld\n",ans);
    }
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值