1. 程式人生 > >POJ1922 Ride to School【模擬+貪心】

POJ1922 Ride to School【模擬+貪心】

Ride to School

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 24162   Accepted: 9617

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 [email protected]

問題連結:POJ1922 Ride to School

問題描述:起點與終點相隔4500米。現Charley 需要從起點騎車到終點。但是,他有個習慣,沿途需要有人陪伴,即以相同的速度, 與另外一個人一起騎。而當他遇到以更快的速度騎車的人時,他會以相應的速度跟上這個更快的人。先給定所有與Charley 同路的人各自的速度與出發時間,問Charley 以這種方式跟人,騎完4500米需要多少時間。得出的結果若是小數,則向上取整。

解題思路:每次Charley選擇的都是當前最快的單車,而且Charley最後一定是和那個最快到達Yanyuan的單車同時到達,因為最早到達的人,一定會趕上查理,而查理就會跟上他一起到達。無視負時間出發的單車(因為Charley還沒到的時候單車就走了,如果速度比他快,他永遠也追不上。速度比他慢的話,追上了也沒影響), 所以我們只要看看比Charley晚出發的單車誰最快到達Yanyuan,Charley最後肯定是跟著這輛車,最後的時間就是Charley到達的時間。

AC的C++程式:

#include<iostream>
#include<algorithm>
#include<cmath>

using namespace std;

int main()
{
	int n;
	while(~scanf("%d",&n)&&n)
	{
		double ans=999999999,vi,ti;
		while(n--)
		{
			scanf("%lf%lf",&vi,&ti);
			if(ti<0) continue;//跳過時間為負的
			double temp=4.5/vi*3600+ti;//跟上這輛車所需的時間
			ans=min(ans,temp);//取短的時間 
		}
		printf("%d\n",(int)ceil(ans));
	}
	return 0;
 }