D. MEX maximizing

Recall that MEX of an array is a minimum non-negative integer that does not belong to the array. Examples:

  • for the array [0,0,1,0,2][0,0,1,0,2] MEX equals to 33 because numbers 0,10,1 and 22 are presented in the array and 33 is the minimum non-negative integer not presented in the array;
  • for the array [1,2,3,4][1,2,3,4] MEX equals to 00 because 00 is the minimum non-negative integer not presented in the array;
  • for the array [0,1,4,3][0,1,4,3] MEX equals to 22 because 22 is the minimum non-negative integer not presented in the array.

You are given an empty array a=[]a=[] (in other words, a zero-length array). You are also given a positive integer xx.

You are also given qq queries. The jj-th query consists of one integer yjyj and means that you have to append one element yjyj to the array. The array length increases by 11 after a query.

In one move, you can choose any index ii and set ai:=ai+xai:=ai+x or ai:=aixai:=ai−x (i.e. increase or decrease any element of the array by xx). The only restriction is that aiai cannot become negative. Since initially the array is empty, you can perform moves only after the first query.

You have to maximize the MEX (minimum excluded) of the array if you can perform any number of such operations (you can even perform the operation multiple times with one element).

You have to find the answer after each of qq queries (i.e. the jj-th answer corresponds to the array of length jj).

Operations are discarded before each query. I.e. the array aa after the jj-th query equals to [y1,y2,,yj][y1,y2,…,yj].

Input

The first line of the input contains two integers q,xq,x (1q,x41051≤q,x≤4⋅105) — the number of queries and the value of xx.

The next qq lines describe queries. The jj-th query consists of one integer yjyj (0yj1090≤yj≤109) and means that you have to append one element yjyj to the array.

Output

Print the answer to the initial problem after each query — for the query jj print the maximum value of MEX after first jj queries. Note that queries are dependent (the array changes after each query) but operations are independent between queries.

Examples
input
Copy
7 3
0
1
2
2
0
0
10
output
Copy
1
2
3
3
4
4
7
input
Copy
4 3
1
2
1
2
output
Copy
0
0
0
0
Note

In the first example:

  • After the first query, the array is a=[0]a=[0]: you don't need to perform any operations, maximum possible MEX is 11.
  • After the second query, the array is a=[0,1]a=[0,1]: you don't need to perform any operations, maximum possible MEX is 22.
  • After the third query, the array is a=[0,1,2]a=[0,1,2]: you don't need to perform any operations, maximum possible MEX is 33.
  • After the fourth query, the array is a=[0,1,2,2]a=[0,1,2,2]: you don't need to perform any operations, maximum possible MEX is 33 (you can't make it greater with operations).
  • After the fifth query, the array is a=[0,1,2,2,0]a=[0,1,2,2,0]: you can perform a[4]:=a[4]+3=3a[4]:=a[4]+3=3. The array changes to be a=[0,1,2,2,3]a=[0,1,2,2,3]. Now MEX is maximum possible and equals to 44.
  • After the sixth query, the array is a=[0,1,2,2,0,0]a=[0,1,2,2,0,0]: you can perform a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3. The array changes to be a=[0,1,2,2,3,0]a=[0,1,2,2,3,0]. Now MEX is maximum possible and equals to 44.
  • After the seventh query, the array is a=[0,1,2,2,0,0,10]a=[0,1,2,2,0,0,10]. You can perform the following operations:
    • a[3]:=a[3]+3=2+3=5a[3]:=a[3]+3=2+3=5,
    • a[4]:=a[4]+3=0+3=3a[4]:=a[4]+3=0+3=3,
    • a[5]:=a[5]+3=0+3=3a[5]:=a[5]+3=0+3=3,
    • a[5]:=a[5]+3=3+3=6a[5]:=a[5]+3=3+3=6,
    • a[6]:=a[6]3=103=7a[6]:=a[6]−3=10−3=7,
    • a[6]:=a[6]3=73=4a[6]:=a[6]−3=7−3=4.
    The resulting array will be a=[0,1,2,5,3,6,4]a=[0,1,2,5,3,6,4]. Now MEX is maximum possible and equals to 77.

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 100007;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

int main()
{
    int q, x,ans=0;
    cin >> q >> x;
    vector<int> a(q+1,0);
    while (q--)
    {
        int t;
        cin >> t;
        a[t%x]++;
        while (a[ans%x])
        {
            a[ans%x]--;
            ans++;
        }
        cout << ans << endl;
    }
    return 0;
}

 

posted @ 2020-04-27 10:55  DeaL57  阅读(181)  评论(0编辑  收藏  举报