1. 程式人生 > >HDU 6215 Brute Force Sorting

HDU 6215 Brute Force Sorting

刪掉 cnblogs code span cstring con class -- 模擬

一層一層刪

鏈表模擬

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
const int maxn = 1e5 + 7;
int que[maxn], a[maxn], pre[maxn], suf[maxn], n, top;
int main()
{
    int _;
    cin >> _;
    while(_--)
    {
        top = 0;
        scanf(
"%d", &n); for(int i = 1; i <= n; i++) { pre[i] = i-1; suf[i] = i+1; scanf("%d", &a[i]); que[top++] = i; } suf[0] = 1; int ans = n, flag = 0; while(1) //一輪一輪的刪, 如果沒有改動過,說明都是有序的了 {
int cnt = 0, cur = 0, flag = 0; while(cur < top) //當前這一輪 { int p = que[cur], num = 0; while(suf[p] <= n && a[p] > a[suf[p]]) //把一個連續的刪掉 { flag = 1; num++; p
= suf[p]; } if(num) { ans -= num+1; suf[pre[que[cur]]] = suf[p]; //維護鏈表,使其聯通 pre[suf[p]] = pre[que[cur]]; que[cnt++] = pre[que[cur]]; } while(que[cur] <= p && cur < top) //修改當前指針 cur++; } top = cnt; if(!flag) break; //沒有就跳出 } printf("%d\n", ans); int cur = 0; while(cur <= n) { if(cur) printf("%d ",a[cur]); cur = suf[cur]; } puts(""); } return 0; }

HDU 6215 Brute Force Sorting