高级农民
- 积分
- 1902
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2018-10-2
- 最后登录
- 1970-1-1
|
本帖最后由 twtypsj 于 2021-5-4 10:18 编辑
从第一个例子来看,我认为是可以呆在原地不动的,因为两个乘客距离出口的距离都是101,假如不能原地不动,那么肯定有一个乘客需要浪费两步让第一个乘客先出去。那答案是103。结果是102,说明第二个乘客在等第一个乘客出去的时候是呆在原地的。他的解释里也说(1-0)+(100-0)= 101, (2-1)+(100-0)= 101,
- /******************************************************************************
- Online C++ Compiler.
- Code, Compile, Run and Debug C++ program online.
- Write your code in this editor and press "Run" button to compile and execute it.
- *******************************************************************************/
- #include <iostream>
- #include <vector>
- #include <algorithm>
- using namespace std;
- int escape(vector<vector<int>> passengers)
- {
- sort(passengers.begin()+1, passengers.end(), [&](auto& a, auto& b){return abs(passengers[0][1]-a[0])+a[1]<abs(passengers[0][1]-b[0])+b[1];});
- int res = 0;
- for(int i=1; i<passengers.size(); ++i)
- res = max(abs(passengers[i][0] - passengers[0][1]) + passengers[i][1], res + 1);
- return res;
- }
- int main()
- {
- cout << escape({
- {2,0},
- {0,100},
- {2,100}
- }) << endl;
- cout << escape({
- {2,1},
- {1,0},
- {1,1}
- }) << endl;
- cout << escape({
- {1,1},
- {1,10}
- }) << endl;
- return 0;
- }
- [i][i][i][i]
复制代码 [/i][/i][/i][/i]
[i][i][i]时间复杂度nlg(n)
求大米
[/i][/i][/i] |
|