标签:printf str ace color r++ tin log algo 这一
一层一层删
链表模拟
#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; }
标签:printf str ace color r++ tin log algo 这一
原文地址:http://www.cnblogs.com/Aragaki/p/7546138.html