HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

Brute Force Sorting

Time Limit: 1 Sec  Memory Limit: 128 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=6215

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]. 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 againHelp 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  1000.
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]<=10000.0.

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 Mintegers 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

HINT

题意

有一个长度为n序列,如果第i(1<=i<=n)位上的值ai<ai-1 || ai>ai+1那么这一位需要被删除。

删除完后,再重复以上操作,直到序列单调不降。

题解:

先考虑暴力,即每次扫一遍数组,删除该删的数,直到不能删为止。

肯定有很多数扫过一遍第二次就可以不用扫了,顺着这个方向想,我们怎么节省扫描次数呢。

我们假设第一次删除了一些数,有些连着被删除的数,我将其称为一段(一个数也算一段),那么我们只需要记住每一段被删除的数的前一个数(未删除的数)即可,(想一想为什么)

将其存入一个队列,下次就直接扫这个队列即可。

具体实践,我们可以用链表记录每个位置的前一个未删除的位置以及后一个未删除的位置。

先把所有点入队,然后对于每个需要删除的点i,nex[i]肯定也要删除,则将last[i]与nex[nex[i]]相连,并将last[i]入队,当然还有一些细节需要处理,具体看代码吧。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define N 1000050
int a[N],n,last[N],nex[N],f[N];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if(c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
} void work()
{
read(n);
nex[]=;
for(int i=;i<=n;i++)
read(a[i]),nex[i]=i+,last[i]=i-;
a[n+]=;
int sum=;
int k=,flag=;
memset(f,,sizeof(f));
queue<int> Q[];
while(!Q[k].empty())Q[k].pop();
while(!Q[-k].empty())Q[-k].pop();
for(int i=;i<=n;i++)Q[k].push(i);
while()
{
flag=;
while(!Q[k].empty())
{
int x=Q[k].front();Q[k].pop();
if (a[x]>a[nex[x]])
{
f[x]=; f[nex[x]]=;
flag=;
nex[last[x]]=nex[nex[x]];
last[nex[nex[x]]]=last[x];
last[nex[x]]=last[x];
if (f[last[x]]==&&(Q[-k].empty()||Q[-k].back()!=last[x]))Q[-k].push(last[x]);
}
}
if (flag==)break;
k=k^;
}
for(int i=;i<=n;i++) if(f[i]==)sum++;
printf("%d\n",sum);
for(int i=;i<=n;i++)
{
if(f[i]==)
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;
read(q);
while(q--)
{
work();
}
return ;
}
上一篇:hdu-5640 King's Cake (水题)


下一篇:javascript排序算法-选择排序