AcWing 3474. 坠落的蚂蚁

    两只本就在动的蚂蚁碰头后交换速度,相当于它们穿过了对方,没有任何变化 因此,在A左边向左和在A右边向右的蚂蚁相当于不会产生任何影响,忽略 A每与其他蚂蚁碰头一次,A就会改变一次方向;如果A左边和右边蚂蚁数量相同,左右影响抵消,A最终会静止;如果数量不相同,A的方向与蚂蚁更多的方向相同,且A从开始到掉落所花的时间与较少边蚂蚁被全部抵消后A碰到较多边的第一只蚂蚁无阻碍走完全部的路程的时间相同(因为速度相等) 如果左边大于右边,时间就是 100减去;如果右边大于左边,时间就是坐标
#include <iostream>
#include <vector>
#include <algorithm>
#define endl 

#define _(a) cout << #a << ": " << (a) << "    "
using namespace std;
typedef pair<int, int> PII;

int main() {
          
   
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    vector<int> l, r;
    vector<PII> ve;
    int n, A;
    cin >> n;
    for (int i = 0, p, op; i < n; ++ i) {
          
   
        cin >> p >> op;
        if (op == 0) A = p;
        ve.push_back({
          
   p, op});
    }
    for (auto x : ve) {
          
   
        if (x.second == 1 && x.first <= A) l.push_back(x.first);
        else if (x.second == -1 && x.first >= A) r.push_back(x.first);
    }
    if (l.size() == r.size()) cout << "Cannot fall!";
    else if (l.size() > r.size()) {
          
   
        sort(l.begin(), l.end());
        cout << 100 - l[l.size() - r.size() - 1];
    } else {
          
   
        sort(r.begin(), r.end());
        cout << r[l.size()];
    }
}
经验分享 程序员 微信小程序 职场和发展