1294D - MEX maximizing - CodeForces Solution


data structures greedy implementation math *1600

Please click on ads to support us..

Python Code:

n, x = map(int, input().split())
arr, result  = [0]*(n), []
mex = 0
for _ in range(n):
    arr[int(input())%x] += 1
    while arr[mex%x]:
        arr[mex%x] -= 1
        mex +=1
    result.append(mex)

print("\n".join(map(str, result)))

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <random>
#include <algorithm>
#include <unordered_map>
#include <map>
using namespace std;

#define int long long

vector<int> smt;

void update(int ver, int nl, int nr, int ind) {
    if (nr - nl == 1) {
        smt[ver] = 1;
        return;
    }
    int nm = nl + (nr - nl) / 2;
    if (ind < nm) update(ver * 2, nl, nm, ind);
    if (ind >= nm) update(ver * 2 + 1, nm, nr, ind);
    smt[ver] = smt[ver * 2] + smt[ver * 2 + 1];
}

int get(int ver, int nl, int nr) {
    if (nr - nl == 1) {
        return nl;
    }
    int nm = nl + (nr - nl) / 2;
    if (smt[ver * 2] == nm - nl) {
        return get(ver * 2 + 1, nm, nr);
    } else {
        return get(ver * 2, nl, nm);
    }
}

void solve() {
    int q, x;
    cin >> q >> x;
    vector<int> ost(x, 0);
    smt.assign(4*(q + 10), 0);
    for (int i = 0; i < q; ++i) {
        int y;
        cin >> y;
        y = y%x + x * ost[y%x];
        ost[y%x]++;
        update(1, 0, (q + 10), y);
        cout << get(1, 0, (q + 10)) << "\n";
    }
}



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


Comments

Submit
0 Comments
More Questions

577A - Multiplication Table
817C - Really Big Numbers
1355A - Sequence with Digits
977B - Two-gram
993A - Two Squares
1659D - Reverse Sort Sum
1659A - Red Versus Blue
1659B - Bit Flipping
1480B - The Great Hero
1519B - The Cake Is a Lie
1659C - Line Empire
515A - Drazil and Date
1084B - Kvass and the Fair Nut
1101A - Minimum Integer
985D - Sand Fortress
1279A - New Year Garland
1279B - Verse For Santa
202A - LLPS
978A - Remove Duplicates
1304A - Two Rabbits
225A - Dice Tower
1660D - Maximum Product Strikes Back
1513A - Array and Peaks
1251B - Binary Palindromes
768B - Code For 1
363B - Fence
991B - Getting an A
246A - Buggy Sorting
884A - Book Reading
1180A - Alex and a Rhombus