1863C - MEX Repetition - CodeForces Solution


implementation

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define FAST cout.tie(0);cin.tie(0);ios::sync_with_stdio(0);
#define int long long
#define mm(x, v) memset(x, v, sizeof(x));
typedef long double ld;
typedef long long ll;
using namespace std;
int mod = 1e9 + 7;


int32_t main()
{
    FAST
    int tt; cin >> tt;
    while(tt--){
        int n,k; cin >> n >> k;
        int a[n + 10];
        int mp[n + 10];
        mm(mp, 0);
        int mex = 0;
        for(int i = 0;i < n; i++){
            cin >> a[i];
            mp[a[i]] = 1;
        }
        if(n == 1){
            if(a[0] != 0){
                cout << (k % 2 ? "0\n" : "1\n");
            }
            else{
                cout << (k % 2 == 0 ? "0\n" : "1\n");
            }
            continue;
        }
        for(int i = 0;i <= n; i++){
            if(!mp[i]){
                mex = i;
                break;
            }
        }
        int pre = a[0];
        a[0] = mex;
        int add = 0;
        int xx;
        for(int i = 1;i <= n; i++) {
            xx = a[i];
            a[i] = pre;
            pre = xx;
        }
        int ans[n];
        k--;
        for(int i = 0;i <= n; i++) ans[(k + i) % (n + 1)] = a[i];
        for(int i = 0;i < n; i++) cout << ans[i] << " ";
        cout << "\n";
    }

}


Comments

Submit
0 Comments
More Questions

20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament
1047B - Cover Points
1381B - Unmerge
1256A - Payment Without Change
908B - New Year and Buggy Bot
979A - Pizza Pizza Pizza
731A - Night at the Museum
742A - Arpa’s hard exam and Mehrdad’s naive cheat
1492A - Three swimmers
1360E - Polygon
1517D - Explorer Space