超清晰代码:
#include<bits/stdc++.h>
#include<deque>
using namespace std;
struct sg{
int zl,bh;
}a;
int e1,sr_e=-1,sl;
deque<sg> q[100005];
queue<int> ans[100005];
int n,f,cnt,e2;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&e1);
a.bh=i;
a.zl=e1;
if(e1!=sr_e){
sl++;
sr_e=e1;
}
q[sl].push_back(a);
}
while(f<sl){
cnt++;
for(int i=1;i<=sl;i++){
if(!q[i].empty()){
e1=q[i].front().bh;
q[i].pop_front();
if(!q[i].empty()){
e2=i-1;
while(q[e2].empty()){
e2--;
if(e2==0) break;
}
if(e2>0&&q[e2].back().zl==q[i].front().zl){
while(!q[i].empty()){
a.bh=q[i].front().bh;
a.zl=q[i].front().zl;
q[i].pop_front();
q[e2].push_back(a);
}
}
}
ans[cnt].push(e1);
if(q[i].empty()) f++;
}
}
}
for(int i=1;i<=cnt;i++){
while(!ans[i].empty()){
printf("%d ",ans[i].front());
ans[i].pop();
}
printf("\n");
}
return 0;
}