Ride to Office(贪心)

【题目描述】

起点与终点相隔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

题目分析:

这个人一旦遇到速度快的人就跟着速度快的跑了,所以他最后一定是跟着最先到达终点的到的。所以我们就可以找这几个人中最快到达终点的,输出他到达时的时刻,这个题就结束了。
对于那些先走的,动动脑子想想都知道,如果追的上他,速度肯定比他快,怎么也不可能跟他走,所以先走的人和这个题一点关系都没有。
这个题应该是要给出速度时间单位的,但是他没给,这就会造成很多不必要的误解,就比如题中的速度单位是千米/小时。而时间单位是秒。所以我们要把速度换成米/秒。最后实际上是v/3.6所以就可以把3.6写道分式上面,写成4500*3.6.

代码

#include <iostream>
#include<bits/stdc++.h>
#define N 1001
#define INF 0x3f3f3f3f
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define rep(i,a,n) for(int i=a;i<=n;i++)
typedef long long ll;
using namespace std;
struct node
{
    int v;
    int start;
    int endd;
}a[N];
int main()
{
    int n;
    int t;
 
    while(cin>>n&&n)
    {
        for(int i=1;i<=n;i++)
        {
            cin>>a[i].v>>a[i].start;
            t=16200/a[i].v;
            if(16200.0/a[i].v>t)
                t++;
            a[i].endd=a[i].start+t;//到达时间
        }
        int min=INF;
        for(int i=1;i<=n;i++)
            if(a[i].start==0&&a[i].endd<min)
                min=a[i].endd;
        for(int i=1;i<=n;i++)
            if(a[i].start>0&&a[i].endd<min)
                min=a[i].endd;
        cout<<min<<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、付费专栏及课程。

余额充值