Files
python/TangDou/CSP-J/J2-2021/4_Queue.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

41 lines
927 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 200010;
struct Node {
int id, v;
};
int n;
queue<Node> q, p;
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
int x;
scanf("%d", &x);
q.push({i, x});
}
int cnt = 0;
while (cnt < n) {
int last = 2; //每一轮,首位都需要输出,所以这里的 flag需要每次初始化为2,黄海最初将它放在while的外面,调试了近半个小时~
while (q.size()) {
auto a = q.front();
q.pop();
if (a.v != last) {
printf("%d ", a.id);
last = a.v;
cnt++;
} else
p.push(a);
}
puts("");
while (p.size()) {
auto a = p.front();
q.push(a);
p.pop();
}
}
return 0;
}