17ACM Qingdao 1010 Brute Force Sorting

来源:互联网 发布:免费.tk域名的注册 编辑:程序博客网 时间:2024/06/06 18:24

题目:http://acm.hdu.edu.cn/showproblem.php?pid=6215
分析:利用类似并查集的方法,记录消除完毕后当前元素的左右元素;消除一个元素就判断消除点左右的元素是否也需要消除。
代码

#include <cstdio>#include <algorithm>#include <cstring>#include <queue>using namespace std;const int Tmax=100005;struct node{    int data,num;};node tmp;int m,A[Tmax],left[Tmax],right[Tmax],now,time,record[Tmax];bool del[Tmax],already[Tmax];queue <node> Q;int findleft(int x){    if(x==0) return 0;    if(del[left[x]]==false) return left[x];    return left[x]=findleft(left[x]);}int findright(int x){    if(x==m+1) return m+1;    if(del[right[x]]==false) return right[x];    return right[x]=findright(right[x]);}void work(){    int l,r,i;    node tip;    while(!Q.empty())    {        tip=Q.front();        Q.pop();        if(time!=tip.num)        {            time=tip.num;            for(i=1;i<=record[0];i++)              del[record[i]]=true;            record[0]=0;        }        l=findleft(tip.data);        r=findright(tip.data);        right[l]=r;        left[r]=l;        if(((A[findleft(l)]>A[l])||(A[findright(l)]<A[l]))&&already[l]==false)        {            already[l]=true;            tmp.data=l;            tmp.num=tip.num+1;            Q.push(tmp);            record[++record[0]]=l;            now--;        }        if(((A[findleft(r)]>A[r])||(A[findright(r)]<A[r]))&&already[r]==false)        {            already[r]=true;            tmp.data=r;            tmp.num=tip.num+1;            Q.push(tmp);            record[++record[0]]=r;            now--;        }    }    return;}int main(){    int T,i;    scanf("%d",&T);    while(T--)    {        while(!Q.empty()) Q.pop();        scanf("%d",&m);        now=m;        time=0;        for(i=1;i<=m;i++)        {            scanf("%d",&A[i]);            left[i]=i-1;            right[i]=i+1;        }        memset(del,0,sizeof(del));        memset(already,0,sizeof(already));        A[0]=-1;        A[m+1]=Tmax;        for(i=1;i<=m;i++)          if(A[i-1]>A[i]||A[i+1]<A[i])           {              tmp.data=i;              tmp.num=1;              Q.push(tmp);              record[++record[0]]=i;              now--;          }        work();        printf("%d\n",now);        for(i=1;i<=m;i++)          if(del[i]==false)            printf("%d ",A[i]);        printf("\n");    }    return 0;}