codeforces A. MEX maximizing

codeforces A. 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] MEX equals to 3 because numbers 0,1 and 2 are presented in the array and 3 is the minimum non-negative integer not presented in the array;
for the array [1,2,3,4] MEX equals to 0 because 0 is the minimum non-negative integer not presented in the array;
for the array [0,1,4,3] MEX equals to 2 because 2 is the minimum non-negative integer not presented in the array.
You are given an empty array a=[] (in other words, a zero-length array). You are also given a positive integer x.

You are also given q queries. The j-th query consists of one integer yj and means that you have to append one element yj to the array. The array length increases by 1 after a query.

In one move, you can choose any index i and set ai:=ai+x or ai:=ai−x (i.e. increase or decrease any element of the array by x). The only restriction is that ai 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 q queries (i.e. the j-th answer corresponds to the array of length j).

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

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

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

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

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

input
4 3
1
2
1
2
output
0
0
0
0

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

MEX - 数组中未出现的最大非负整数
给定及部分时更要注意和整数x,一次query往里头加一个数yj
一次操作可以选择数组中任何一个数+/- x,且操作后ai不能为负
可以操作任意次
最大化MEX

知识点&算法

题干大概的意思就是不断把数加入一个空数组,每次加入可以对数组里所有数进行±x的操作,然后找到当前步骤下最大的MEX。
因为操作的次数是没有限定的,意味着一个数可以变成同余x的任何数。而且随着数的不断加入,当前最大的MEX是严格不减的。
所以我们只要从0开始枚举MEX。
如果当前数组里存在和枚举到的MEX同余的数,就说明MEX可以达到,这是将数组里的这个数删去,并将MEX++。
由于实际上是同余,所以拿数组来统计同余的数的个数就可以了,删去即-1。

题解

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
int n,m,k,res,__,cnt[400005],num,a,b,c,d,x,y,sum,ave,diffX,diffY;
string ans = "";

int main()
{
    scanf("%d %d",&__,&x);
    while(__--){
        scanf("%d",&num);
        cnt[num%x]++;
        while(cnt[res%x]){
            --cnt[res%x];
            ++res;
        }
        cout<<res<<endl;
    }
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值