Hdu 6215 Brute Force Sorting【链表】

来源:互联网 发布:阿里云对象存储oss 编辑:程序博客网 时间:2024/06/05 12:49

Brute Force Sorting

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


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[i1].
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 (1T10) 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 1A[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
551 2 3 4 555 4 3 2 151 2 3 2 151 3 5 4 252 4 1 3 5
 

Sample Output
51 2 3 4 5 021 2 21 3 32 3 5


题目大意:


给出长度为N的一个序列,其中如果有位子不满足:a【i-1】<=a【i】<=a【i+1】的话,这个位子i上的数这一轮游戏就需要将其删除掉。

进行若干轮游戏,直到最终一轮游戏没有可以被删除的数为止,让我们求出最终序列的样子。


思路:


我们知道,如果有一个位子使得a【i】>a【i+1】的话,那么a【i】和a【i+1】这一轮游戏都需要删除,那么我们知道,我们删除了这两个数之后,可能会有a【i-1】>a【i+2】,那么就会带来下一轮的删除操作。所以我们能够知道,我们如果删除了区间【L,R】之后,我们只需要考虑a【L-1】和a【R+1】是否还能继续删除即可,所以每一次我们删除的部分只会影响下一次删除的附近两个位子,那么我们过程模拟链表即可。


Ac代码:

#include<stdio.h>#include<string.h>#include<queue>using namespace std;int a[150000];int nex[150000];int pre[150000];int flag[150000];int main(){    int t;scanf("%d",&t);    while(t--)    {        queue<int>s,q;        memset(flag,0,sizeof(flag));        int n;scanf("%d",&n);        for(int i=1;i<=n;i++)scanf("%d",&a[i]);        for(int i=1;i<=n;i++)        {            pre[i]=i-1;            nex[i]=i+1;            q.push(i);        }        while(1)        {            int tmp=0;            while(!q.empty())            {                int u=q.front();q.pop();                if(u>=1&&u<=n)                s.push(u);            }            while(!s.empty())            {                int u=s.front();s.pop();                if(u>=1&&u<=n&&nex[u]>=1&&nex[u]<=n&&a[u]>a[nex[u]])                {                    if(flag[u]==1||flag[nex[u]]==1)continue;                    int ss=u;                    int v=nex[u];                    while(1)                    {                        if(u>=1&&u<=n&&v>=1&&v<=n)                        {                            tmp=1;                            if(a[u]>a[v])                            {                                flag[u]=flag[v]=1;                                nex[pre[ss]]=nex[v];                                pre[nex[v]]=pre[ss];                            }                            else break;                        }                        else break;                        u=nex[u];                        v=nex[u];                    }                    q.push(pre[ss]);                }            }            if(tmp==0)break;        }        int cnt=0;        for(int i=1;i<=n;i++)if(flag[i]==0)cnt++;        printf("%d\n",cnt);        for(int i=1;i<=n;i++)        {            if(flag[i]==0)            printf("%d ",a[i]);        }        printf("\n");    }}






原创粉丝点击