题目链接:https://www.luogu.com.cn/problem/P5250
分析:
根据题目的描述,可以想到用set集合作为仓库,set提供了很多函数,如count可以返回集合中是否存在某一元素,存在则为1,不存在则为0,lower_bound可以返回集合中第一个大于等于指定元素的元素的下标,upper_bound则返回集合中第一个大于指定元素的元素的下标,erase可以根据下标删除元素。
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
using namespace std;
int main()
{
int n;
scanf("%d",&n);
int op,x;
set<int> s;
while(n--) {
scanf("%d%d",&op,&x);
if(op == 1) {
if(s.count(x)) printf("Already Exist\n");
else s.insert(x);
} else {
if(s.empty()) printf("Empty\n");
else {
set<int>::iterator i,j;
i = s.lower_bound(x);
j = i;
if(i != s.begin()) j--;
if(i != s.begin() && abs(*j - x) <= abs(*i - x)) i--;
printf("%d\n",*i);
s.erase(i);
}
}
}
return 0;
}