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
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
题意:定义 如果 a[i] 满足 a[i]<a[i-1] || a[i+1]<a[i] 则 称 a[i]为 nusort。每次把数列里的 nusort 删掉,然后合并 剩下的段, 继续操作 直到不能没有unsort存在。
分析; 把 所以 下降 子段 删除 。然后剩下的段 用 链表 连接 ,每次 把 每一个段的最前一个元素a[r] 与 在他的前一个段的 最后一个 元素a[l] 比较大小 如果 a[l] > a[r] 则把 a[l]与a[r]都删除掉。因为会有 某些段会变成空 ,所以 合并段 在一个进行此操作,直到没有 unsort存在。
#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
const int maxn = 1000000;
const int inf = 100000000;
typedef long long ll;
using namespace std;
int n,a[maxn+5],L[maxn+5],R[maxn+5],per[maxn+5],nex[maxn+5],vis[maxn+5];
void solve()
{
int cnt = 0;
a[0] = -inf , a[n+1] = inf;
for(int i=1; i<=n; i++) if(a[i]<a[i-1]||a[i+1]<a[i]) vis[i] = 0;//删除 下降子段
vis[n+1] = 0;
for(int i=1,l=0; i<=n+1; i++)
{
l+=vis[i];
if(vis[i]==0&&l) L[++cnt] = i-l, R[cnt] = i-1,l=0,per[cnt] = cnt-1,nex[cnt] = cnt+1;// 合并段
}
int flag = 1;
nex[0] = 1;
while(flag)
{
flag = 0;
for(int i=nex[0]; i<=cnt; i=nex[i])// 找出 nusort, 并删掉。
{
int l = R[per[i]], r = L[i];
if(a[r]<a[l])
{
flag = 1;
vis[r] = 0, vis[l] = 0;
L[i]++,R[per[i]]--;
}
}
int l = 0;
for(int i=1; i<=cnt; i=nex[i]) // 合并段
{
if(L[i]>R[i]) continue;
per[i] = l,nex[l] = i, l = i;
}
nex[l] = cnt+1;
}
int num = 0;
for(int i=1; i<=n; i++) if(vis[i]) num++;
printf("%d\n",num);
for(int i=1; i<=n; i++) if(vis[i]) printf("%d ",a[i]);
printf("\n");
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]),vis[i]=1;
solve();
}
return 0;
}