'commit'
This commit is contained in:
41
TangDou/CSP-J/J2-2021/4_Queue.cpp
Normal file
41
TangDou/CSP-J/J2-2021/4_Queue.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#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;
|
||||
}
|
||||
Reference in New Issue
Block a user