HDU 6215 Brute Force Sorting(双向链表+队列)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/GYH0730/article/details/82689510

Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2304    Accepted Submission(s): 539

Problem Description

Beerus needs to sort an array of N integers. Algorithms are not Beerus's strength. Destruction is what he excels. He can destroy all unsorted numbers in the array simultaneously. A number A[i] of the array is sorted if it satisfies the following requirements.
1. A[i] is the first element of the array, or it is no smaller than the left one A[i−1].
2. A[i] is the last element of the array, or it is no bigger than the right one A[i+1].
In [1,4,5,2,3], for instance, the element 5 and the element 2 would be destoryed by Beerus. The array would become [1,4,3]. If the new array were still unsorted, Beerus would do it again.
Help Beerus predict the final array.

Input

The first line of input contains an integer T (1≤T≤10) which is the total number of test cases.
For each test case, the first line provides the size of the inital array which would be positive and no bigger than 100000.
The second line describes the array with N positive integers A[1],A[2],⋯,A[N] where each integer A[i] satisfies 1≤A[i]≤100000.

Output

For eact test case output two lines.
The first line contains an integer M which is the size of the final array.
The second line contains M integers describing the final array.
If the final array is empty, M should be 0 and the second line should be an empty line.

Sample Input

5 5 1 2 3 4 5 5 5 4 3 2 1 5 1 2 3 2 1 5 1 3 5 4 2 5 2 4 1 3 5

Sample Output

5 1 2 3 4 5 0 2 1 2 2 1 3 3 2 3 5

Source

2017 ACM/ICPC Asia Regional Qingdao Online

删除数组中的降序序列,形成一个新数组,在删除新数组中的降序序列,直至数组中不存在降序序列,每次只从开头遍历寻找降序序列肯定会T,我们可以用一个双向链表来模拟整个过程,用一个队列存储有可能产生降序序列的头节点(开始把所有节点都加进去),从队列里取出一个节点,判断它后面的序列是否为降序,是则把这一段序列删除,用双向链表维护就行了,同时把头节点的上一个节点加入队列,直到某一次数组不存在降序序列退出

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100005;
int que[MAXN],last[MAXN],Next[MAXN],a[MAXN];
int main(void)
{
    int T,n,top;
    scanf("%d",&T);
    while(T--) {
        top = 0;
        scanf("%d",&n);
        for(int i = 1; i <= n; i++) {
            last[i] = i - 1;
            Next[i] = i + 1;
            scanf("%d",&a[i]);
            que[top++] = i;
        }
        Next[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(Next[p] <= n && a[p] > a[Next[p]]) {
                    flag = 1;
                    num++;
                    p = Next[p];
                }
                if(num) {
                    ans -= (num + 1);
                    Next[last[que[cur]]] = Next[p];
                    last[Next[p]] = last[que[cur]];
                    que[cnt++] = last[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 = Next[cur];
        }
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/GYH0730/article/details/82689510
今日推荐