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:=ai−xai:=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 (1≤q,x≤4⋅1051≤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 (0≤yj≤1090≤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
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]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=10−3=7a[6]:=a[6]−3=10−3=7,
a[6]:=a[6]−3=7−3=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.
思路:其实+x以及-x,就是相应的取余操作->%x。
对于所有的输入,我们先将它取余x,这样就将它降到了题目要求范围内的最小值。然后我们就不断的用当前的MEX取余x,如果MEX取余x存在的话,就说明MEX可以由某一个值通过+x来得到,那么这个MEX就应该+1。不断的这么操作来得到最大值。
代码如下:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

int x,n;

int main()
{
	cin>>n>>x;
	map<int,int> mp;//用map导致差点超时,x的取值不是很大,因此我们可以采用数组。
	int ans=0;
	int z;
	for(int i=1;i<=n;i++)
	{
		cin>>z;
		mp[z%x]++;
		while(mp[ans%x])
		{
			mp[ans%x]--;
			ans++;
		}
		cout<<ans<<endl;
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值