题解 | #坠落的蚂蚁#

坠落的蚂蚁

https://www.nowcoder.com/practice/fdd6698014c340178a8b1f28ea5fadf8

千万别模拟,千万别模拟,千万别模拟,重要的事情说三遍,模拟涉及细节太多了,判定碰撞,交换速度,判断是否掉落,只能另找他法。思路:夜尽天明AR

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  struct node {
    int pos, step;
  };
  vector<node> vec(n);
  node it;
  for (auto &v : vec) {
    cin >> v.pos >> v.step;
    if (v.step == 0) {
      it = v;
    }
  }
  auto p = remove_if(vec.begin(), vec.end(), [&it](node &v) {
    return v.pos < it.pos && v.step < 0 || v.pos > it.pos && v.step > 0;
  });
  sort(vec.begin(), p, [](auto x, auto y) { return x.pos < y.pos; });
  int left = 0, right = 0;
  for (auto x = vec.begin(); x != p; x++) {
    if ((*x).pos > it.pos) {
      right++;
    }
    if ((*x).pos < it.pos) {
      left++;
    }
  }
  if (left == right) {
    cout << "Cannot fall!";
  } else if (left < right) {
    cout << vec[2 * left + 1].pos;
  } else {
    cout << 100 - vec[left - right - 1].pos;
  }
  return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务