hdu 6215 Brute Force Sorting(模拟)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215

题解:类似双链表的模拟。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int M = 1e5 + ;
int a[M] , Next[M] , pre[M] , que[M];
int main() {
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
Next[] = ;
pre[n + ] = n;
int tot = ;
for(int i = ; i <= n ; i++) {
scanf("%d" , &a[i]);
que[tot++] = i;
Next[i] = i + ;
pre[i] = i - ;
}
int flag = ;
int ans = n;
while(!flag) {
flag = ;
int pos = ;
int useful = ;
while(pos < tot) {
int now = que[pos];
int cnt = ;
while(Next[now] <= n) {
if(a[now] > a[Next[now]]) now = Next[now] , cnt++ , flag = ;
else break;
}
if(cnt) {
ans -= (cnt + );
Next[pre[que[pos]]] = Next[now];
pre[Next[now]] = pre[que[pos]];
que[useful++] = pre[que[pos]];
}
while(que[pos] <= now && pos < tot) pos++;
}
tot = useful;
}
printf("%d\n" , ans);
int now = Next[];
while(ans--) {
printf("%d " , a[now]);
now = Next[now];
}
printf("\n");
}
return ;
}
上一篇:Linux查看磁盘空间大小命令


下一篇:排序算法 - 选择排序(selection sort)