您的位置:首页 > 其它

解题报告BLOG开启。。第一题 PKU1922 Ride to School

2009-03-07 18:09 519 查看
PKU1922
Ride to School
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 9621 Accepted: 3789

Description
Many graduate students of Peking University are living in Wanliu Campus, which is 4.5 kilometers from the main campus – Yanyuan. Students in Wanliu have to either take a bus or ride a bike to go to school. Due to the bad traffic in Beijing, many students choose to ride a bike.

We may assume that all the students except "Charley" ride from Wanliu to Yanyuan at a fixed speed. Charley is a student with a different riding habit – he always tries to follow another rider to avoid riding alone. When Charley gets to the gate of Wanliu, he will look for someone who is setting off to Yanyuan. If he finds someone, he will follow that rider, or if not, he will wait for someone to follow. On the way from Wanliu to Yanyuan, at any time if a faster student surpassed Charley, he will leave the rider he is following and speed up to follow the faster one.

We assume the time that Charley gets to the gate of Wanliu is zero. Given the set off time and speed of the other students, your task is to give the time when Charley arrives at Yanyuan.

Input
There are several test cases. The first line of each case is N (1 <= N <= 10000) representing the number of riders (excluding Charley). N = 0 ends the input. The following N lines are information of N different riders, in such format:

Vi [TAB] Ti

Vi is a positive integer <= 40, indicating the speed of the i-th rider (kph, kilometers per hour). Ti is the set off time of the i-th rider, which is an integer and counted in seconds. In any case it is assured that there always exists a nonnegative Ti.

Output
Output one line for each case: the arrival time of Charley. Round up (ceiling) the value when dealing with a fraction.

Sample Input

4
20 0
25 -155
27 190
30 240
2
21 0
22 34
0

Sample Output

780
771

Source
Beijing 2004 Preliminary@POJ

*/

#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
class a
{
public:
double speed;
double begintime;
};

double solution(vector<a> k)
{
double alltimes;
int sign = 0;
double road = 4.5;
for(vector<a>::const_iterator iter = k.begin();iter != k.end();iter++)
{

if((*iter).begintime >= 0)
{
if(sign == 0)
{
sign = 1;
alltimes = (road) / (*iter).speed * 3600 + (*iter).begintime;
}
if((road) / (*iter).speed * 3600 + (*iter).begintime < alltimes)
{
alltimes = (road) / (*iter).speed * 3600 + (*iter).begintime;
}

}
}

return ceil(alltimes);

}
int main(void)
{
int N;
int i;
while(cin >> N)
{
if(N > 0)
{
vector<a> k;
a m;
for(i = 0;i < N;i++)
{
cin>>m.speed>>m.begintime;
k.push_back(m);
}
cout<<solution(k)<<endl;

}
else
{
break;
}
}
Charley
return 0;
}

弄明白题目里的窍门很重要。。。这个题目本来的想法是开始先遍历开始时间最短且>0的。。那么 Charley就跟着这个。。接着再从剩下的人里遍历找出最快赶上当前 Charley的。。一直这样的过程。。
于是问题出来了。。。如果以这样的算法绝对要超过1000MS了

认真看题目可以发现。。。。最后 Charley一定是和一个人同时到达的。。。并且 从上面的分析可以知道Charley在骑车过程中的选择一直都是最优的。。也就是他这样选择的结果实际上就是找到了一个最快到达目的地的人。。。那么实际上只要遍历所有开始时间为>=0的人且到达时间最短的那个就是 Charley到达的时间。算法的复杂度是O(N)。

另外需要注意的是遍历的时候开始时间为负的人不用管,因为他们永远不会“赶上” Charley,还有必须是>=0,因为有可能不只是 Charley的开始时间为0,有可能当 Charley刚到开始地点的时候刚好也有别人在那, Charley就会跟着这个人。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: