2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010

#include
#include
#include
#include
#include
#include<set>
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
#define bug(x) cout<<"bug"<#define PI acos(-1.0)
#define eps 1e-8
const int N=1e5+100,M=1e5+100;
const int inf=0x3f3f3f3f;
const ll INF=1e18+7,mod=1e9+7;
int a[N];
int pre[N],ne[N];
bool vis[N],inq[N];
queue<int>q;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        memset(vis,false,sizeof(vis));
        memset(inq,false,sizeof(inq));
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&a[i]);
            pre[i]=i-1,ne[i]=i+1;
        }
        a[0]=0,a[n+1]=inf;
        ne[0]=1,pre[n+1]=n;
        for(int i=1; i<=n; i++)
        {
            int s=pre[i];
            while(i<=n&&!(a[i-1]<=a[i]&&a[i]<=a[i+1]))
            {
                vis[i]=true;
                i++;
            }
            if(s==pre[i]) continue;
            ne[s]=i,pre[i]=s;
            if(1<=s&&s<=n&&!inq[s]) q.push(s),inq[s]=true;
            if(1<=i&&i<=n&&!inq[i]) q.push(i),inq[i]=true;
        }
        while(!q.empty())
        {
            int i=q.front();
            inq[i]=false;
            q.pop();
            if(vis[i]) continue;
            int s=pre[i];
            while(i<=n&&!(a[pre[i]]<=a[i]&&a[i]<=a[ne[i]]))
            {
                vis[i]=true;
                i=ne[i];
            }
            if(s==pre[i]) continue;
            ne[s]=i,pre[i]=s;
            if(1<=s&&s<=n&&!inq[s]) q.push(s),inq[s]=true;
            if(1<=i&&i<=n&&!inq[i]) q.push(i),inq[i]=true;
        }
        int ans=0;
        for(int i=0; ine[i])
            if(ne[i]<=n) ans++;
        printf("%d\n",ans);
        for(int i=0; ine[i])
            if(ne[i]<=n) printf("%d ",a[ne[i]]);
        printf("\n");
    }
    return 0;
}
模拟

 

转载于:https://www.cnblogs.com/Roni-i/p/7536368.html

你可能感兴趣的:(2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010)