Ride to Office

描述
Many staff of are living in a place called MZone, far from their office( 4.5 km ). Due to the bad traffic, many staff choose to ride a bike.

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

We assume the time that Weiwei gets to the gate of MZone is zero. Given the set off time and speed of the other people, your task is to give the time when Weiwei arrives at his office.
输入
There are several test cases. The first line of each case is N (1 <= N <= 10000) representing the number of riders (excluding Weiwei). 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 one line for each case: the arrival time of Weiwei. Round up (ceiling) the value when dealing with a fraction.
样例输入
4
20 0
25 -155
27 190
30 240
2
21 0
22 34
0
样例输出
780
771

这道题目被翻歪了。

起点与终点相隔4500米。现Charley 需要从起点骑车到终点。但是,他有个习惯,沿途需要有人陪伴,即以相同的速度, 与另外一个人一起骑。而当他遇到以更快的速度骑车的人时,他会以相应的速度跟上这个更快的人。先给定所有与Charley 同路的人各自的速度与出发时间,问Charley 以这种方式跟人,骑完4500米需要多少时间。得出的结果若是小数,则向上取整。

中文题目中没有提到车速是每小时多少公里。

一、分析

一开始我觉得既然维维走的速度是由陪伴人决定的,而陪伴人的速度已经决定,所以可以模拟过程,然而我WRONG ANSWER了,然后我放弃了
于是换了一种方法,仔细想一想,其实只需算出出发时间>=0的陪伴人到达终点的时间就可以了,因为如果陪伴人出发时间>=0并且最先到达的话那么他肯定是追上来的,也就是说维维会跟上他,并且到达时间最短
而出发时间<0的陪伴人如果先到达,那么维维肯定追不上
如果后到达,维维不会跟着他
所以只用考虑>=0 的就行了
然后代码就变得简单了
二、代码

#include<bits/stdc++.h>
using namespace std;
int v,t;
int main()
{
int n;
while(cin>>n&&n!=0)
{
int ans=99999,s=0;
for(int i=1;i<=n;i++)
{
cin>>v>>t;
if(t>=0)
{
double Time=(4500/(v*1000.0/3600.0));
s=ceil(Time)+t;
if (ans>s)ans=s;

}

}

printf("%d\n",ans);
}
return 0;
}

三、反思

时间复杂度:O(n)

这道题目思路的关键点就是贪心策略,是建立在”他会以相应的速度跟上这个更快的“意思是比他的速度更快,并在他到达终点时跟他一起到达。转化为到点时间点的最小值问题。

如果这道题目改改,不是以相应的速度跟上这个更快的人。而是以前面一个人一样的速度跟骑呢??就是一动态的过程,后面出发的人可能在一段时间内影响主人公的速度,等于要求区间速度,在多少米内以什么速度,再另外的米内以另外的速度。有点意思。区间的起终点都要计算。我们可以试着出这样的题。

posted @ 2019-06-13 10:18  玛克人(Macren)  阅读(325)  评论(0)    收藏  举报