使用Set做的,样例过但是3WA 2TLE
查看原帖
使用Set做的,样例过但是3WA 2TLE
1086872
ImSakura楼主2023/12/30 17:09
#include <bits/stdc++.h>
using namespace std;
set<long> sets;

int main() {
	long n = 0;
	cin >> n;
	for (long i = 1; i <= n; i++) {
		long a, b;
		cin >> a >> b;
		//Input
		if (a == 1) {
			if (sets.find(b) == sets.end())
				sets.insert(b);
			else
				cout << "Already Exist" << endl;
		}
		//Output
		if (a == 2) {
			if (sets.empty()) {
				cout << "Empty" << endl;
			} else {
				//Find
				if (sets.find(b) != sets.end()) {
					cout << b << endl;
					sets.erase(b);
				}
				else {
					long record_1 = 0;
					long record_2 = 0;

					//往左
					for (long i = b - 1; i >= 1; i--) {
						if (sets.find(i) != sets.end()) {
							record_1 = i;
							break;
						}
					}
					//往右
					for (long i = b + 1; i <= *sets.rbegin(); i++) {
						if (sets.find(i) != sets.end()) {
							record_2 = i;
							break;
						}
					}
					
					//判断谁最接近, 当一样接近时,输出短的那个
					if (abs(b - record_1) <= abs(record_2 - b)) {
						cout << record_1 << endl;
						sets.erase(record_1);
					} else {
						cout << record_2 << endl;
						sets.erase(record_2);
					}
				}
			}
		}
	}
	return 0;
}

样例可以过,但是提交是前3个WA 后面俩个TLE 求大佬帮助

2023/12/30 17:09
加载中...