哈理工校园编程练习赛杭电 acm 4442 E.Physical Examination
Physical Examination
Time Limit: 2000/1000 MS(Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6161 Accepted Submission(s): 1758
Problem Description
WANGPENG is a freshman. He is requested to have a physical examination whenentering the university.
Now WANGPENG arrives at the hospital. Er….. There are so many students, and thenumber is increasing!
There are many examination subjects to do, and thereis 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 todetermine an exam sequence, so that he can finish all
the physical examinationsubjects as early as possible.
Input
There are several test cases. Each test case starts with a positive integer nin a line, meaning the number of subjects(queues).
Then n lines follow. The i-th line has a pair ofintegers (ai, bi) to describe the i-th queue:
1. If WANGPENG follows this queue at time 0, WANGPENGhas to wait for ai seconds to finish this subject.
2. As the queue is getting longer, the waiting timewill 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<231.
Output
For each test case, output one line with an integer: the earliest time (countedby seconds) that WANGPENG can finish all exam subjects. Since WANGPENG isalways 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 givenorder. He spends 1 second in the first queue, 5 seconds in the 2th queue, 27seconds in the 3th queue,
169 seconds in the 4th queue, and 1217 seconds inthe 5th queue. So the total time is 1419s. WANGPENG has computed all possibleorders in his
120-core-parallel head, and decided that this isthe optimal choice.
Source
2012 Asia JinHua Regional Contest
这个题压根基本理解意思 但比赛的时候,感觉理解的比较模糊,无从下手。用到了结构;
题意: 排n支队伍, 每支队伍排队时间ai, 每支队伍会随时间增加都增长. 求最短排队时间.
解题思路:
1. 设当前结果的时间time, 每次选择都有(a1, b1), (a2, b2).
选择1队列先的耗时:time1 = (time+a1+b1*time + a2+b2*(time+a1+b1*time));
选择2队列先的耗时:time2 = (time+a2+b2*time + a1+b1*(time+a2+b2*time));
time1<= time2 化简得: a1*b2 <= a2*b1.
2. 从小到大排序, 依次去一支队伍计算时间就是最小值. 最后要注意使用__int64
or long long.
另一种直观的思路是:仔细想想ai的时间不会变,变得是每组利用时间的成长空间大小,会发现如果bi越大的,越不赶紧去排队到最后会需要排很长时间的,
因为此次耗时=ai+bi*上次耗时。
#include<iostream>
#include<algorithm>
usingnamespace std;
constint M=100005;
structshu
{
_int64 a,b;
}a[M];
boolcmp(shu &a,shu &b)
{
return a.a*b.b<a.b*b.a;
}
intmain()
{
int n;
_int64 mod=365*24*60*60;
while(cin>>n,n)
{
for(int i=0;i<n;i++)
{
scanf("%I64d%I64d",&a[i].a,&a[i].b);
}
sort(a,a+n,cmp);
_int64 tot=0;
for( int j=0;j<n;j++)
{
_int64ans=(a[j].a+a[j].b*tot);
tot+=ans;
tot%=mod;
}
printf("%I64d\n",tot);
}
return 0;
}