奆佬求助!过了样例,造了数据,为何全WA?
查看原帖
奆佬求助!过了样例,造了数据,为何全WA?
1073710
csq_pig楼主2024/3/18 22:43
#include <bits/stdc++.h>
using namespace std;
map <int,int> mp;
int n,x,y;
int main()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++) {
		scanf("%d%d",&x,&y);
		if (x==1) {
			if (mp[y]!=0) printf("Already Exist\n");
			else mp[y]=1;
		}
		else {
//			cout<<"map 还有如下元素 : "; 
//			for (map <int,int>::iterator it=mp.begin();it!=mp.end();it++)
//				cout<<it->first<<" ";
//			cout<<endl;
			if (mp.empty())
				printf("Empty\n");
			else if (mp[y]) {
				printf("%d\n",y);
				mp.erase(y);
			}
			else {
				mp[y]=1;
				map <int,int>::iterator it=mp.find(y),nxt=it,bef=it;
				if (it!=mp.begin()) bef--;
				if (it!=mp.end()) nxt++;
				int a=bef->first, b=it->first, c=nxt->first;
//				cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
				if (it==mp.begin())
					printf("%d\n",c),mp.erase(c);
				else if (it==mp.end())
					printf("%d\n",a),mp.erase(a);
				else if (c-b==b-a)
					printf("%d\n",a),mp.erase(a);
				else if (c-b>b-a)
					printf("%d\n",a),mp.erase(a);
				else if (c-b<b-a)
					printf("%d\n",c),mp.erase(c);
				mp.erase(y);
			}
		}
	}
	return 0;
}
/*
10
1 5
1 3
1 7
2 6
2 5
1 7
1 8
2 7
2 7
2 8
*/
2024/3/18 22:43
加载中...