题目地址

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

题干

so easy
so easy

代码和解释

本题需要逆向思维。如果是存储所有元素(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\n",tmp);
					break;
				}
				tmp++;
				if(tmp>n){
					printf("%d\n",-1);
					break;
				}
			}
		}
	}
	return 0; 
}

相关文章:

  • 2021-05-17
  • 2021-06-21
  • 2021-12-20
  • 2022-12-23
  • 2018-07-08
  • 2021-11-09
  • 2021-07-09
猜你喜欢
  • 2022-01-21
  • 2021-11-07
  • 2021-06-18
  • 2021-11-01
  • 2022-12-23
  • 2021-12-29
  • 2021-06-13
相关资源
相似解决方案