题目链接

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
2 3 5
 
 

题意: 有一个长为 n 的数列a[1]~a[n],现在对这个数列进行删除操作,如果 a[i]>a[i+1] 则删去 a[i] 和 a[i+1],进行完一轮之后如果还有不满足的数,则进行第二轮删除操作,求最后剩下的数的个数,并且输出这些数?

思路: 使用双向链表进行模拟,对于每一个节点记录其左边的节点和右边节点的下标,首先把1~n的下标都放入队列中,每次从队列中取出一个下标now , pre=a[now].l , next=a[now].r , 判断a[now].x<=a[next].x , 如果满足则表示合理,从队列中取下一个数;否则将 pre 放入队列,因为现在要模拟删除 now 和 next ,删除之后可能pre和下一个节点大小关系就不合理了,所以需要放入队列中,另外还要进行

a[pre].r=a[next].r;
         a[a[next].r].l=pre;
         a[next].l=pre;

前两个很明显表示删除now和next,最后一个a[next].l=pre,是因为可能next也在队列中,下一个要判断是否删除 next 和 a[next].r , 删除之后都需要将链表前后连接起来,例如对于n=5 , 2 5 3 1 4这样的数据,删除5和3数值后(对应的下表为2和3,下标从1开始),下一个3和1也不满足。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=1e5+;
struct Node
{
int l,r;
int x;
}a[N];
queue<int>Q; int main()
{
int T; cin>>T;
while(T--)
{
int n; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].l=i-;
a[i].r=i+;
Q.push(i);
}
a[].r=; a[n+].x=;
while(!Q.empty())
{
int now=Q.front(); Q.pop();
int pre=a[now].l;
int next=a[now].r;
if(a[now].x>a[next].x)
{
Q.push(pre);
a[pre].r=a[next].r;
a[a[next].r].l=pre;
a[next].l=pre;
}
}
int ans=;
int now=a[].r;
while(now<=n)
{
ans++;
now=a[now].r;
}
printf("%d\n",ans);
now=a[].r;
while(now<=n)
{
printf("%d ",a[now].x);
now=a[now].r;
}
puts("");
}
return ;
}

最新文章

  1. apache.commons.io.IOUtils: 一个很方便的IO工具库(比如InputStream转String)
  2. jQuery笔记总结
  3. Reed-Solomon码,QR
  4. webapi输入验证过滤器ValidationActionFilter
  5. transition实现自定义tips淡入淡出效果
  6. 六款值得推荐的android(安卓)开源框架简介
  7. Nginx HTTP负载均衡和反向代理配置
  8. GRE红宝书5-6
  9. DIRECTORY_SEPARATOR
  10. windows文件快速搜索软件推荐
  11. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Server新特性
  12. 递归遍历XML所有节点
  13. jdk8中tomcat修改配置PermSize为MetaspaceSize
  14. rename_windows.go
  15. Mybaits-plus实战(一)
  16. 使用Python正则表达式自己实现解析URL各参数
  17. jmeter的介绍和使用一
  18. GCC 命令行详解 -L 指定库的路径 -l 指定需连接的库名 zhuan
  19. PetaPoco源代码学习--3.Sql类
  20. resource not found :rgbd_launch

热门文章

  1. 分享一个图片上传插件(TP5.0)
  2. Java 数组扩容
  3. SSM之整合Redis
  4. 第4章 同步控制 Synchronization ----信号量(Semaphore)
  5. stdafx.h 的作用
  6. [Oracle]理解undo表空间
  7. Android打包版本号设置
  8. JavaWeb(一)Servlet中乱码解决与转发和重定向的区别
  9. ch7-列表渲染(v-for key 数组更新检测 显示过滤/排序结果)
  10. ch1-vuejs基础入门(hw v-bind v-if v-for v-on v-model 应用组件简介 小案例)