STLmap,0分求助!
查看原帖
STLmap,0分求助!
238310
guosoun楼主2021/11/26 20:55

用了map中的lower_bound与upper_bound来解决拿走与此木棍最相近的木棍

#include <bits/stdc++.h>
using namespace std;
map<int, int>mp;
int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		int f,l;
		cin >>f>>l;
		if(f==1){
			if(mp.count(l)) cout<<"Already Exist"<<endl;
			else mp[l]=1;
		}
		else{
			if(!mp.size()){
				cout<<"Empty"<<endl;
				continue;
			}
			int fr=INT_MIN+l;
			if(mp.lower_bound(l)!=mp.begin()){
				fr=(*(--mp.lower_bound(l))).first;
			}
			int bh=INT_MAX-l;
			if(mp.upper_bound(l)!=mp.end()){
				fr=(*(mp.upper_bound(l))).first;
			}
			if(mp.count(l)){
				cout<<l;
				mp.erase(l);
			}
			else if(l-fr<=bh-l){
				cout<<fr;
				mp.erase(fr);
			}
			else{
				cout<<bh;
				mp.erase(bh);
		
			}
			cout<<endl;
		}
	}
}
2021/11/26 20:55
加载中...