ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Codeforces Round #615 (Div. 3) D. MEX maximizing

2020-01-23 12:44:22  阅读:466  来源: 互联网

标签:operations maximizing perform Codeforces 615 query array include MEX


原题:
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
inputCopy
7 3
0
1
2
2
0
0
10
outputCopy
1
2
3
3
4
4
7
inputCopy
4 3
1
2
1
2
outputCopy
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.
题意:
q次询问,每次放进一个数(开始为空数组),可以对数组内的某个数或者某几个数做任意操作数的加x或者减x (x为input第二个数),处理完后询问,在该数组情况下的最小整数,就是数组里的数都被排除的情况下,从0开始找,第一个不在数组里的整数就是了,然后要求操作加x或者减x的任意操作后使得那个询问结果最大化。
思路:水题啊!!!明显是要让他尽可能覆盖从0开始的整数,把小的整数盖掉才会得到大的结果,如果小的那个比如说1,已经被覆盖,然后给你一个数它是1+nx,毫无疑问这个数%x得1,然后从1开始找,1已经有了,就1+1x覆盖,以此类推就行了!本题问题主要是方法乱容易超时,数据大容易runtime error。
题目链接

#include<iostream>
#include<vector>
#include<set>
#include<string>
#include<map>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<string> 
#include<iomanip>
#include<queue>
#include<vector>
#include<set> 
#include<cstring>
using namespace std;
int f[400005];
bool a[400005];
int main (){
	int q,x,ans=0;
	scanf("%d%d",&q,&x);
	for(int i=1;i<=q;i++){
		int y;
		scanf ("%d",&y);
		y%=x;
		unsigned long long dd=y+1*x*f[y];//无unsigned会喜提runtime error 
		if(dd<=q)
		a[dd]=true;
		f[y]++;//f[y]是%出来的余数的位置,记录这个体系的出现了几次,然后可以直接乘,就省时间 
		while(a[ans]) 
		ans++;
		printf("%d\n",ans);
	}
	return 0;
}
星痕一梦 发布了5 篇原创文章 · 获赞 0 · 访问量 162 私信 关注

标签:operations,maximizing,perform,Codeforces,615,query,array,include,MEX
来源: https://blog.csdn.net/FYXZZX/article/details/104074951

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有