![](https://i-blog.csdnimg.cn/direct/12884b89080745038c12263e2a389221.jpeg)
![](https://i-blog.csdnimg.cn/direct/850d02974cb5426585ce0127706ae805.png)
![](https://i-blog.csdnimg.cn/direct/ce44ada5d4b245bea4ca993e211404ee.png)
![](https://i-blog.csdnimg.cn/direct/9c399490140e4a25be4070ac4c1f1ede.png)
![](https://i-blog.csdnimg.cn/direct/b76c75e92c5f4e1eae2136b09e3c73b0.png)
![](https://i-blog.csdnimg.cn/direct/a13b100bdc564d5f9b0dd550323f0391.png)
![](https://i-blog.csdnimg.cn/direct/a5cd8c6005e34188bf05ec05d5a951c5.png)
![](https://i-blog.csdnimg.cn/direct/928b6f07473c46d5be9e6c64d53d9ef2.png)
![](https://i-blog.csdnimg.cn/direct/dd3d859a0cdd41bf9cf6653f26e982f0.png)
![](https://i-blog.csdnimg.cn/direct/dd3311a680654d5d8ae0b59264d82df0.png)
![](https://i-blog.csdnimg.cn/direct/823f994694dd4d01b1e1eaf87cad2649.png)
![](https://i-blog.csdnimg.cn/direct/626a1d3eeebf45198d5fcdabe1f8b903.png)
#include <bits/stdc++.h>using namespace std;struct Ship {int u; // 从地球到火星的时间int v; // 从火星到天王星的时间
};// 自定义比较函数
bool cmp(const Ship &a, const Ship &b) {return a.u + max(a.v, b.u) + b.v < b.u + max(b.v, a.u) + a.v;
}int main() {// 使用 freopen 进行文件输入输出//freopen("travel.in", "r", stdin); // 输入文件//freopen("travel.out", "w", stdout); // 输出文件int N; // 飞船数量cin >> N;vector<Ship> ships(N);// 输入飞船的时间数据for (int i = 0; i < N; ++i) {cin >> ships[i].u >> ships[i].v;}// 使用自定义比较函数进行排序sort(ships.begin(), ships.end(), cmp);// 输出飞船的排序结果 /*for (int i = 0; i < N; ++i) {cout << ships[i].u << " " << ships[i].v << endl;}*/int earth_departure_time = 0; // 从地球出发的时间int mars_departure_time = 0; // 从火星出发的时间int total_time = 0; // 总时间// 贪心算法计算总时间for (const auto &ship : ships) {// 更新从地球出发的时间earth_departure_time += ship.u;// 计算从火星出发的时间mars_departure_time = max(earth_departure_time, mars_departure_time) + ship.v;// 更新总时间为当前飞船到达天王星的时间total_time = mars_departure_time;}// 输出最终的总时间cout << total_time << endl;return 0;
}