1705E - Mark and Professor Koro - CodeForces Solution


binary search bitmasks brute force combinatorics data structures greedy *2300

Please click on ads to support us..

C++ Code:

#include <iostream>
#include <cstdio>
#include <climits>
#include <cmath>
#include <cstring>
#include <string>
#include <array>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <algorithm>
#include <functional>

using namespace std;

#define ll long long
#define vi vector<int>
#define ssz(a) (int)((a).size())

#define endl '\n'
template <class T> void in(vector<T>& a) { for (int i = 0; i < ssz(a); i++) cin >> a[i]; }
template <class T> void out(const vector<T>& a) { for (int i = 0; i < ssz(a); i++) cout << a[i] << " \n"[i + 1 == ssz(a)]; }

void solve() {
    int n, q; cin >> n >> q;
    vi a(n); in(a);

    set<int, greater<int>> pos, neg;

    function<void(int)> add = [&] (int x) {
        if (neg.count(x)) {
            neg.erase(x);
            return;
        }
        if (pos.count(x)) {
            pos.erase(x);
            add(x + 1);
            return;
        }
        pos.insert(x);
    };

    function<void(int)> remove = [&] (int x) {
        if (pos.count(x)) {
            pos.erase(x);
            return;
        }
        if (neg.count(x)) {
            neg.erase(x);
            remove(x + 1);
            return;
        }
        neg.insert(x);
    };

    for (auto x : a) {
        add(x);
    }

    while (q--) {
        int k, l; cin >> k >> l;
        k--;

        remove(a[k]);
        a[k] = l;
        add(a[k]);

        while (!neg.empty() && *neg.begin() == *pos.begin() - 1) {
            int x = *neg.begin();
            remove(x + 1);
            add(x);
            add(x);
        }

        int ans = *pos.begin();
        if (ssz(pos) == 1 && !neg.empty()) {
            ans--;
        }
        else if (ssz(pos) > 1 && !neg.empty() && *neg.begin() > *next(pos.begin())) {
            ans--;
        }
        cout << ans << endl;
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t = 1;
    // cin >> t;
    while (t--) solve();
}


Comments

Submit
0 Comments
More Questions

1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array