提交时间:2024-03-26 16:40:28

运行 ID: 139890

#include<bits/stdc++.h> using namespace std; int main(){ int N(0); cin >> N; vector<int> num(N), nxt(N, -1), head, tail; queue<int> heads; for (int i(0), cur(0); i != N; ++i) { cin >> num[i]; if (!i || num[i - 1] != num[i]) { heads.push(head.size()); if (i) tail.push_back(i - 1); head.push_back(i); } else nxt[i - 1] = i; } tail.push_back(N - 1); while (!heads.empty()) { int sz = heads.size(); while (sz--) { int cur(heads.front()); heads.pop(); cout << head[cur] + 1 << ' '; head[cur] = nxt[head[cur]]; if (~head[cur] && heads.back() < cur && num[head[cur]] == num[head[heads.back()]]) { nxt[tail[heads.back()]] = head[cur]; tail[heads.back()] = tail[cur]; head[cur] = -1; } if (~head[cur])heads.push(cur); } cout << endl; } return 0; }