69852023-12-23 20:12:43MagyarKendeSZLGSzínezéscpp17Accepted 50/5057ms4416 KiB
#include <iostream>
#include <vector>
using namespace std;
#define speed cin.tie(0); ios::sync_with_stdio(0)

int main() {
    speed;

    int N, Q;
    cin >> N >> Q;
    vector<bool> balls(N);
    for (int i = 0; i < N; i++) {
        char c;
        cin >> c;
        balls[i] = c == '1';
    }

    int z_sum = 0, o_sum = 0, i = 0;
    while (i < N) {
        if (balls[i]) o_sum++;
        else z_sum++;
        do {
            i++;
        } while (i < N && balls[i - 1] == balls[i]);
    }

    cout << min(z_sum, o_sum) << '\n';

    while (Q--) {
        if (N == 1) {
            cout << "0\n";
            continue;
        }

        int p;
        cin >> p;
        p--;
        if (!p) {
            if (balls[p + 1]) {
                z_sum += (balls[p] ? 1 : -1);
            } else {
                o_sum += (balls[p] ? -1 : 1);
            }
        }
        else if (p == N - 1) {
            if (balls[p - 1]) {
                z_sum += (balls[p] ? 1 : -1);
            } else {
                o_sum += (balls[p] ? -1 : 1);
            }
        }
        else if (balls[p - 1] == balls[p + 1]) {
            if (balls[p - 1] == balls[p]) {
                z_sum++;
                o_sum++;
            } else {
                z_sum--;
                o_sum--;
            }
        }

        balls[p] = !balls[p];

        cout << min(z_sum, o_sum) << '\n';
    }
}
SubtaskSumTestVerdictTimeMemory
base50/50
1Accepted0/03ms1832 KiB
2Accepted0/03ms2032 KiB
3Accepted2/23ms2384 KiB
4Accepted2/23ms2500 KiB
5Accepted3/33ms2812 KiB
6Accepted3/33ms2880 KiB
7Accepted3/33ms3140 KiB
8Accepted3/33ms3100 KiB
9Accepted2/23ms3260 KiB
10Accepted2/23ms3260 KiB
11Accepted3/357ms3496 KiB
12Accepted3/357ms3620 KiB
13Accepted3/354ms3836 KiB
14Accepted3/356ms3832 KiB
15Accepted3/354ms3840 KiB
16Accepted3/357ms3920 KiB
17Accepted3/357ms4128 KiB
18Accepted3/352ms4328 KiB
19Accepted3/352ms4240 KiB
20Accepted3/354ms4416 KiB