​Aimee​

set练手

insert有一个pair的返回值,second代表插入成功没有

s .end()返回值是最后元素的下一个位置。

lowerbound是第一个大于等于

upperbound是最后一个小于等于

#include<set>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<iostream>
using namespace std;
int n;
int x,y;
set<int> s;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&x,&y);
if(x==1){
if(!s.insert(y).second){
printf("Already Exist\n");
}
}else{
if(s.empty()){
printf("Empty\n");
}else{
if(s.find(y)!=s.end()){
printf("%d\n",y);
s.erase(s.find(y));
}else{
set<int>::iterator pl=s.lower_bound(y);
set<int>::iterator pl1=pl;
set<int>::iterator pl2=pl1;
if(pl==s.begin()){
cout<<*pl;
s.erase((pl));
}else if(pl==s.end()){
cout<<*(--pl);
s.erase(pl);
}
else{
if(y-*(--pl1)<=*(pl2)-y){
cout<<*(pl1);
s.erase(pl1);
}else{
cout<<*(pl);
s.erase(pl);
}
}
cout<<endl;
}

}
}
}
return 0;
}