2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> #include<set> #include<bitset> #include<map> #include<queue> #include<stack> #include<vector> using namespace std; typedef long long ll; typedef pair<int,int> P; #define bug(x) cout<<"bug"<<x<<endl; #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; i<n; i=ne[i]) if(ne[i]<=n) ans++; printf("%d\n",ans); for(int i=0; i<n; i=ne[i]) if(ne[i]<=n) printf("%d ",a[ne[i]]); printf("\n"); } return 0; }