题解 | #坠落的蚂蚁#
坠落的蚂蚁
https://www.nowcoder.com/practice/fdd6698014c340178a8b1f28ea5fadf8
#include <algorithm> #include <cstdio> #include <vector> #include <iostream> using namespace std; const int MAX = 100 ; struct P{ int pos; int v; } a[MAX]; bool cmp(P p1, P p2){ return p1.pos < p2.pos; } /** 图解: https://blog.nowcoder.net/n/ef050f9ef47945c693c7cca6dbcd4b6e */ int main() { int n; int A; cin >> n; vector<P> pl; vector<P> pr; for (int i = 0; i < n; i ++) { scanf("%d%d", &a[i].pos, &a[i].v); if(a[i].v == 0) A = i; } for(int i = 0; i < n; i ++){ if (a[i].pos < a[A].pos && a[i].v > 0) { pl.push_back(a[i]); }else if(a[i].pos > a[A].pos && a[i].v < 0){ pr.push_back(a[i]); } } if (pl.size() == pr.size()) { cout << "Cannot fall!" << endl; }else{ sort(pl.begin(), pl.end(), cmp); sort(pr.begin(), pr.end(), cmp); if (pl.size() > pr.size()) { cout << 100 - pl[pl.size() - pr.size() - 1].pos << endl;//99的时候还没掉下去,故还需要再走1步 }else{ cout << pr[pl.size()].pos << endl; } } return 0; } // 64 位输出请用 printf("%lld")