HDU 6215 Brute Force Sorting【双端链表】

题目链接
题意:给出一个序列,从左到右扫一遍,每个数如果比左面相邻的小或者比右边相邻的大,就删掉,先不更新,最后扫过一遍统一更新,更新完后的序列重复操作,直至满足没有可以删除的项。
思路:
最开始想着维护一个K值(当前队列的顶端值),一遍扫过出答案,wa了。
根据题目模拟去写一遍一遍的去扫序列,用双端链表去更新序列,更新后重复操作,最后输出链表上的值。

#include <cstdio>
#include <cstring>
#define MAXN 100010
int a[MAXN], que[MAXN], next[MAXN], last[MAXN];

int main() {
    int t, n;
    scanf("%d", &t);
    while(t--) {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a[i]);
            last[i] = i - 1; //数组模拟链表 
            next[i] = i + 1;
            que[i - 1] = i;
        }
        last[n] = n + 1, next[0] = 1;
        int top = n, ans = n;
        bool flag = true;
        while(flag) { //多次遍历更新的序列,直到不可删除 
            flag = false;
            int now = 0, s = 0;
            while(now < top) { //遍历序列 
                int i = que[now], res = 0;
                while(next[i] <= n && a[i] > a[next[i]]) {
                    res++;
                    i = next[i];
                    flag = true;
                }
                if(res) { //更新序列,重点 
                    ans -= res + 1;
                    next[last[que[now]]] = next[i];
                    last[next[i]] = last[que[now]];
                    que[s++] = last[que[now]];
                }
                while(que[now] <= i && now < top) now++;
            }
            top = s; //新序列的个数 
        }
        int now = 0;
        printf("%d\n", ans);
        while(now <= n) {
            if(now != 0) printf("%d ", a[now]);
            now = next[now];
        } 
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值