so easy

题目地址

so easy(The Preliminary Contest for ICPC Asia Xuzhou 2019)

题干


代码和解释

本题需要逆向思维。如果是存储所有元素(1e9),然后删除not available的元素或设置标签,那么会超时。所以我们只存not available的元素(1e6),然后判断元素是否被存在 set 里已确定其是否available。
c++代码如下:

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n,q;
	int z,x;
	set<int> a;//从小到大,不可用的元素集合 
	set<int>::iterator it;
	int tmp;
	scanf("%lld%lld",&n,&q);
	while(q--){
		scanf("%d%d",&z,&x);
		if(z==1){
			a.insert(x);
		}
		else if(z==2){
			tmp=x;
			while(1){
				if(!a.count(tmp)){//tmp可用 
					printf("%d
",tmp);
					break;
				}
				tmp++;
				if(tmp>n){
					printf("%d
",-1);
					break;
				}
			}
		}
	}
	return 0; 
}
原文地址:https://www.cnblogs.com/hardcoreYutian/p/11485897.html