当前位置:网站首页>“蔚来杯“2022牛客暑期多校训练营7 F

“蔚来杯“2022牛客暑期多校训练营7 F

2022-08-10 19:07:00 eyuhaobanga

F

祖玛游戏,相邻相同可删,相邻之和=x可删

AC代码:

#include <bits/stdc++.h>
#define rep(i,a,n) for(int i=a;i<n;i++)
using namespace std;
using LL = long long;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, x;
    cin >> n >> x;
    deque<int> qu;
    int ans = 0;
    for (int i = 0; i < n; i++) {
        int c;
        cin >> c;
        if (i != 0) {
            int z;
            if (!qu.empty()) {
                z = qu.back();
            }
            else {
                qu.push_back(c);
                continue;
            }
            if (z == c or z + c == x) {
                qu.pop_back();
                ans++;
            }            
            else {
                qu.push_back(c);
            }
        }
        else {
            qu.push_back(c);
        }
    }
    int z1, z2;
    bool ok = true;
    while (qu.size() >= 2 and ok) {
        ok = false;
        z1 = qu.front(), z2 = qu.back();
        if (z1 + z2 == x or z1 == z2) {
            qu.pop_front();
            qu.pop_back();
            ans++;
            ok = true;
        }
    }
    cout << ans << '\n';

    return 0;
}

原网站

版权声明
本文为[eyuhaobanga]所创,转载请带上原文链接,感谢
https://blog.csdn.net/eyuhaobanga/article/details/126243621