queue + map 80 分TLE!
  • 板块P9518 queue
  • 楼主zjr0330
  • 当前回复0
  • 已保存回复0
  • 发布时间2023/8/13 11:32
  • 上次更新2023/11/3 04:07:45
查看原帖
queue + map 80 分TLE!
746947
zjr0330楼主2023/8/13 11:32

蒟蒻打了一百多行3个小时还没想出来(悲

#include <bits/stdc++.h>
using namespace std;

queue<string> q;
int n, lk = 0;
string sp[500000];
map<string, string>fid;

void re(string kl) {
	queue<string> tmp;
	while (!q.empty()) {
		if (q.front() == kl) {
			q.pop();
		} else {
			tmp.push(q.front());
			q.pop();
		}
	}
	while (!tmp.empty()) {
		q.push(tmp.front());
		tmp.pop();
	}
}

bool ist(string name) {
	if (fid.find(name) != fid.end())return true;
	else return false;
}

void pd(string name) {
	if (!ist(name)) {
		cout << "Error" << endl;
		return;
	}
	re(name);
	cout << "OK" << endl;
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	string pying1 = "", pying2 = "";
	for (int i = 0; i < n; i++) {
		string event;
		cin >> event;
		if (event == "start") {
			if (pying1 != "") {
				q.push(pying1);
				pying1 = "";
			}
			if (pying2 != "") {
				q.push(pying2);
				pying2 = "";
			}
			if (q.empty()) {
				cout << "Error" << endl;
			} else {
				string p1 = q.front();
				q.pop();
				if (!q.empty()) {
					string p2 = q.front();
					q.pop();
					cout << p1 << " " << p2 << endl;
					pying2 = p2;
				} else {
					cout << p1 << endl;
				}
				pying1 = p1;
//				q.push(p1);
//				if (!q.empty()) {
//					q.push(q.front());
//					q.pop();
//				}
			}
		} else if (event.substr(0, 6) == "arrive") {
			cin >> event;
			string name = event;
			if (name == pying1 || name == pying2) {
				cout << "Error" << endl;
			} else {
				if (ist(name) != true) {
					fid[name] = name;
					cout << "OK" << endl;
					q.push(name);
				} else {
					cout << "Error" << endl;
				}
			}
		} else if (event.substr(0, 5) == "leave") {
			cin >> event;
			string name = event;
			if (pying1 == name || pying2 == name) {
				cout << "Error" << endl;
			} else {
				pd(event);
				fid.erase(name);
			}
		}
	}
	return 0;
}
2023/8/13 11:32
加载中...