社区讨论

80 pts, #9, #10 T了,求优化

P9518queue参与者 5已保存回复 8

讨论操作

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

当前回复
8 条
当前快照
1 份
快照标识符
@lo1vb0ms
此快照首次捕获于
2023/10/23 03:34
2 年前
此快照最后确认于
2023/11/03 04:04
2 年前
查看原帖
CPP
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iomanip>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <set>
#include <algorithm>
using namespace std;
typedef long long ll;
template <typename T>
void read(T &x) {
	x = 0;
	int f = 1;
	char ch = getchar();
	while (!isdigit(ch)) {
		if (ch == '-') f = -f;
		ch = getchar();
	}
	while (isdigit(ch)) {
		x = x * 10 + (ch - '0');
		ch = getchar();
	}
	x *= f;
}
template <typename T>
void print(T x) {
	if (x < 0) x = -x, putchar('-');
	if (x < 10) putchar(x + '0');
	else {
		print(x / 10);
		putchar(x % 10 + '0');
	}
}
int n, f = 1, r, lst;//f队头下标,r队尾下标,lst指上次start人数
string s, q[1000010];
map<string, int> t, id;//t表示当前人的状态:0为不在队中,1为在普通排队,2为在特殊排队(玩);id指这个人存在q中的下标
int main() {
//	freopen(".in", "r", stdin);
//	freopen(".out", "w", stdout);
	read(n);
	while (n--) {
		cin >> s;
		if (s == "start") {
			if (f > r) {
				puts("Error");
				continue;
			}
			if (lst) {//如果不是第一次start
				t[q[f]] = 1, q[++r] = q[f++], id[q[f - 1]] = r;
				if (lst > 1) t[q[f]] = 1, q[++r] = q[f++], id[q[f - 1]] = r;
			}//1或2人去队尾
			cout << q[f];
			t[q[f]] = 2;
			lst = 1;
			if (r - f >= 1) cout << " " << q[f + 1], t[q[f + 1]] = 2, lst = 2;
			putchar(10);//1或2人标记,输出
		} else if (s == "arrive") {
			cin >> s;
			if (t[s] > 0) puts("Error");
			else puts("OK"), t[s] = 1, q[++r] = s, id[s] = r;//入队,状态为1,记好下标
		} else {//求助重点
			cin >> s;
			if (t[s] != 1) puts("Error");
			else {
				puts("OK");
				for (int i = id[s] + 1; i <= r; ++i) q[i - 1] = q[i], --id[q[i]];
				--r;
				t[s] = 0;
			}
		}
		q[r + 1] = "";
	}
	return 0;
}
题解区不太能搞懂,求问用上述方法如何不 T:((((((((((((((

回复

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

正在加载回复...