codeforces 1294 D. MEX maximizing thinking

Title effect: For each set of data, and a query q can be any number of addition and subtraction of x, for each query, a number can be added to the array, the number of all addition and subtraction can be any number of times x, each query Q, the array the smallest non-negative integer may not exist is the maximum number.

Ideas: For each array of digital insertion, since addition and subtraction may be performed any number of times x, then that other numbers and this number can be obtained by adding or subtracting x are equivalent in their axes equidistant above, by plus or minus x can reach each other. So they found can be summarized by y% x.
Then each time recording this did not appear to find the best value in the array ans, ans directly from the can start looking back at the time of the next query, based on if a [ans% x]! = 0, Description other figures may reach this position by adding or subtracting x. If it is equal to 0, the position can not be reached by conventional digital subtraction in which x is naturally possible maximum current situation. (When satisfying a [ans% x]! = 0 , it should consume a a [ans% x] for reaching this position).

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxx = 4e5+10;
ll ans,q,x,y;
ll a[maxx];
int main ()
{
    ios::sync_with_stdio(false);
    cin>>q>>x;
    ans = 0;
    while(q--){
        cin>>y;
        a[y%x]++;
        while(a[ans%x]){
            a[ans%x]--;
            ans++;
        }
        cout<<ans<<endl;
    }
    return 0;
}

Published 22 original articles · won praise 3 · Views 515

Guess you like

Origin blog.csdn.net/yp0413170331/article/details/104088438