Ride to Office

1227:Ride to Office

时间限制: 1000 ms 内存限制: 65536 KB
提交数: 2778 通过数: 1607

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

【输入】
输入若干组数据,每组数据第一行n(1≤n≤10000),n为0,表示输入结束,接着输入n行数据,每行2个数据,表示速度v和出发时间t,如果t<0,表示陪伴人提早出发了。

【输出】
输出对应若干行数据,每行输出1个数,表示最快到达的时间。

【输入样例】
4
20 0
25 -155
27 190
30 240
2
21 0
22 34
0
【输出样例】
780
771
【来源】

No
刚一看,两眼发蒙,额,脑子疼。
但是,如果追上了先出发的人,则先出发的人速度慢,不必跟随,如果追不上,也与其没有关系。如果跟随后出发的人被后后出发的人追上,就跟随后后出发的人,那么后后出发的人走过相同的路程相比后出发的人走过相同的路程所节省的时间恰好等于其出发时间相较与后出发的人的出发时间所延迟的时间。即相当于一开始就跟随后后出发的人所花费的时间。

#include <iostream>
#include<cmath>
using namespace std;
int main()
{
    int n;
    int a;
    int b;
    double ttime;
    while(cin>>n)
    {
        if(n==0) return 0;
        int ans=10000000;
        for(int i=1;i<=n;i++)
        {
            cin>>a>>b;
            if(b<0) continue;
            ttime=ceil((4500/(a*1.0/3.6))+b);
            if(ans>ttime) ans=ttime;
        }
        cout<<ans<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To solve this problem, we can use the concept of relative speed. Let's consider the scenario step by step: 1. First, we need to find the person who sets off the earliest among all the riders. Let's call this person "earliestRider". The time when Weiwei arrives at his office will be equal to the set off time of "earliestRider" plus the time it takes for Weiwei to catch up with "earliestRider". 2. Next, we calculate the relative speed between Weiwei and "earliestRider". If Weiwei is faster than "earliestRider", he will catch up with them before they reach the office. Otherwise, Weiwei will need to wait for someone faster to catch up to him. 3. Once Weiwei catches up with "earliestRider", we update the set off time of "earliestRider" to the time when Weiwei catches up with them. 4. We repeat steps 1-3 until Weiwei reaches his office. Here's a sample C++ code that implements this logic: ```cpp #include <iostream> #include <vector> struct Rider { int setOffTime; int speed; }; int main() { int n; // number of riders (excluding Weiwei) std::cout << "请输入骑行人数(不包括Weiwei):"; std::cin >> n; std::vector<Rider> riders(n); std::cout << "请依次输入每位骑行人的出发时间和速度:" << std::endl; for (int i = 0; i < n; i++) { std::cin >> riders[i].setOffTime >> riders[i].speed; } int weiweiSpeed; std::cout << "请输入Weiwei的速度:"; std::cin >> weiweiSpeed; int weiweiArrivalTime = 0; while (true) { int earliestRiderIndex = -1; int earliestRiderTime = INT_MAX; // Find the earliest rider for (int i = 0; i < n; i++) { if (riders[i].setOffTime < earliestRiderTime) { earliestRiderTime = riders[i].setOffTime; earliestRiderIndex = i; } } // Calculate the time Weiwei takes to catch up with the earliest rider double timeToCatchUp = static_cast<double>(4.5) / (weiweiSpeed - riders[earliestRiderIndex].speed); if (earliestRiderTime + timeToCatchUp <= 60) { weiweiArrivalTime = earliestRiderTime + timeToCatchUp; riders[earliestRiderIndex].setOffTime += timeToCatchUp; } else { break; // Weiwei arrives at or after 60 minutes, stop the loop } } std::cout << "Weiwei到达办公室的时间为:" << weiweiArrivalTime << "分钟" << std::endl; return 0; } ``` In this code, we first input the number of riders (excluding Weiwei) and their set off times and speeds. Then we input Weiwei's speed. The program iterates until Weiwei arrives at or after 60 minutes, finding the earliest rider, calculating the time to catch up with them, and updating their set off time accordingly. Finally, it outputs Weiwei's arrival time at the office. Please note that this is a simplified implementation and doesn't handle all possible edge cases. You can modify and improve it based on your specific requirements. Let me know if you have any further questions!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值