题意
你从位置 出发,每秒最多走 米。
有 颗草莓,第 颗位于位置 ,在时刻 成熟(之前不能采摘)。采摘不耗时。
目标是:在所有草莓都成熟后将其采摘,并最终回到位置 ,求最少总时间。
思路
- 你可以全部在返程采摘。
去程时路过草莓位置,但不必立即采摘。
只要在返回途中经过该位置时,时间 它的成熟时间 ,就可以采摘。 - 考虑最优路径:从 出发,向东走到某个最大位置 ,然后返回 。
返回时经过 的时间是 。
必须满足 ,即 。 - 因为你必须走到最远的草莓处,所以 ,所以来回就是 。
综上,最小总时间
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long ll;
ll a,b,n,max1,max2;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i=0;i<n;i++){
cin >> a >> b;
max1=max(max1,a);
max2=max(max2,a+b);
}
ll ans=max(2*max1,max2);
cout << ans << endl;
return 0;
}