社区讨论

奆佬求助!过了样例,造了数据,为何全WA?

P5250【深基17.例5】木材仓库参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@ltx24z08
此快照首次捕获于
2024/03/18 22:45
2 年前
此快照最后确认于
2024/03/19 13:10
2 年前
查看原帖
CPP
#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
*/

回复

0 条回复,欢迎继续交流。

正在加载回复...