第二周--贪心题解(P)

题P:
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

题意:
简化而言。一个学生从 地点A 到 地点B,他在起点处需要等一个人一起走,且过程中如果发现有人速度更大,则更换跟的人。A B两地相距4.5千米。

注:输入的是速度单位是千米每小时,而每个同学出现的时间是以秒为单位。

思路:
既然每次都跟着速度最大的人。题目中一直引导需要考虑过程中某时间更换跟的人,但本题恰恰只需要要考虑重点的情况。当到终点时,他必然跟着的是速度最大的那个人,而那个人到达的时间就是这个同学到达的时间。
所以只需考虑最先到达的人。注意加上那个人的出发时间,再进行排序。

代码:

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

using namespace std;
struct stud
{
    int a1;
    int b;
    double c;
}a[10005];
bool cmp( stud a3,stud a2)
{
    return a3.c<a2.c;
}


int main()
{
    int n;

    while(cin>>n&&n!=0)
    {
        for(int i=0;i<n;i++)
        {
            cin>>a[i].a1>>a[i].b;
            a[i].c=a[i].b+4.5/a[i].a1*1.0*3600;
        }
        sort(a,a+n,cmp);
        int j,i;
        for(i=0;i<n;i++)
        {
            if(a[i].b>=0) {j=a[i].c;break;}
        }
        if(j!=a[i].c) j+=1;
        cout<<j<<endl;
    }
    return 0;
}

总结:
1.注意单位换算。
2.考虑问题可以从两个极端开始考虑。比如此题,考虑出发的情况,会发现不同的人出发时间不同,但并不意味着出发晚的到达的晚;考虑到达后的情况,会发现第一个到达的人,必然跟着那个同学,得出只需求出最先到达的人时间即可。
3.然后就是细节问题,出发时间为负数的不给予考虑,因为那个同学是从时间0出发的。
然后就是谨以的问题,数据类型要为double。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值