12
返回列表 发新帖
楼主: salvadorsyc
跳转到指定楼层
上一主题 下一主题
收起左侧

[其他] escape on the train求助

🔗
twtypsj 2021-5-4 10:10:28 | 只看该作者
全局:
本帖最后由 twtypsj 于 2021-5-4 10:18 编辑

从第一个例子来看,我认为是可以呆在原地不动的,因为两个乘客距离出口的距离都是101,假如不能原地不动,那么肯定有一个乘客需要浪费两步让第一个乘客先出去。那答案是103。结果是102,说明第二个乘客在等第一个乘客出去的时候是呆在原地的。他的解释里也说(1-0)+(100-0)= 101,  (2-1)+(100-0)= 101,



  1. /******************************************************************************

  2.                               Online C++ Compiler.
  3.                Code, Compile, Run and Debug C++ program online.
  4. Write your code in this editor and press "Run" button to compile and execute it.

  5. *******************************************************************************/

  6. #include <iostream>
  7. #include <vector>
  8. #include <algorithm>
  9. using namespace std;

  10. int escape(vector<vector<int>> passengers)
  11. {
  12.     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];});
  13.     int res = 0;
  14.     for(int i=1; i<passengers.size(); ++i)
  15.         res = max(abs(passengers[i][0] - passengers[0][1]) + passengers[i][1], res + 1);
  16.     return res;
  17. }

  18. int main()
  19. {
  20.     cout << escape({
  21.         {2,0},
  22.         {0,100},
  23.         {2,100}
  24.     }) << endl;
  25.     cout << escape({
  26.         {2,1},
  27.         {1,0},
  28.         {1,1}
  29.     }) << endl;
  30.     cout << escape({
  31.         {1,1},
  32.         {1,10}
  33.     }) << endl;
  34.     return 0;
  35. }

  36. [i][i][i][i]
复制代码
[/i][/i][/i][/i]

[i][i][i]时间复杂度nlg(n)
求大米

[/i][/i][/i]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表