队列 + map 40pts 求调
  • 板块P9518 queue
  • 楼主卷王慢即快
  • 当前回复8
  • 已保存回复8
  • 发布时间2023/8/13 11:11
  • 上次更新2023/11/3 04:08:07
查看原帖
队列 + map 40pts 求调
494699
卷王慢即快楼主2023/8/13 11:11

一开始 4040 分 WA 没过,看了题解后还是还无头绪啊(

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline int read() {
	int x = 0, f = 1; char ch = getchar();
	while(ch < '0' || ch > '9') { if(ch == '-') f = -1; ch = getchar(); }
	while(ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); }
	return x * f;
}
int n;
map<string, int> place;
//未出现是 0,队伍是 1,机厅是 2
queue<string> q; //队伍
string lstx = "#", lsty = "#";
inline void push(string x) {
	if(x == "#") return;
	q.push(x);
	place[x] = 1;
}
int main() {
	speed: std::ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) {
		string s; cin >> s;
		if(s == "arrive") {
			string x; cin >> x;
			if(place[x] == 1 || place[x] == 2) cout << "Error\n";
			else {
				q.push(x);
				place[x] = 1;
				cout << "OK\n";
			}
		}
		else if(s == "leave") {
			string x; cin >> x;
			if(place[x] == 0 || place[x] == 2) cout << "Error\n";
			else {
				place[x] = 0;
				cout << "OK\n";
			}
		}
		else {
			push(lstx); push(lsty);
			string tmpx = "#", tmpy = "#";
			if(q.empty()) { cout << "Error\n"; continue; }
			
			string now = q.front();
			while(!q.empty() && place[now] == 0) { q.pop(); now = q.front(); }
			if(q.empty() && place[now] == 0) { cout << "Error\n"; continue; }
			place[now] = 2; tmpx = now; q.pop();
			
			if(!q.empty()) {
				now = q.front();
				while(!q.empty() && place[now] == 0) { q.pop(); now = q.front(); }
				q.pop();
				if(!q.empty() || place[now] != 0) place[now] = 2, tmpy = now;
			}
			lstx = tmpx, lsty = tmpy;
			
			if(lstx != "#") cout << lstx << " ";
			if(lsty != "#") cout << lsty;
			cout << "\n";
		}
	}
	return 0;
}
2023/8/13 11:11
加载中...