P5250 【深基17.例5】木材仓库

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
");
			}
		}else{
			if(s.empty()){
				printf("Empty
");
			}else{
				if(s.find(y)!=s.end()){
					printf("%d
",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;
}
原文地址:https://www.cnblogs.com/For-Miku/p/15116830.html