Brute Force Sorting

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1204    Accepted Submission(s): 314

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

 

Source

 
 //2017-09-19
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ; int a[N], n;
int _next[N], _pre[N];
int head, tail, que[N<<]; int main()
{
int T;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
tail = ;
for(int i = ; i <= n; i++){
scanf("%d", &a[i]);
_next[i] = i+;
_pre[i] = i-;
que[tail++] = i;
}
a[] = ;
a[n+] = N;
_pre[] = ;
_next[] = ;
_pre[n+] = N;
_next[n+] = n+;
int ans = n, fg = , ptr;
while(fg){
fg = ;
head = ;
ptr = ;
while(head < tail){
int cnt = , i = que[head];
for(; i <= n; i = _next[i]){
if(a[i] > a[_next[i]]){
cnt++;
fg = ;
}else break;
}
if(cnt){
ans -= cnt+;
_next[_pre[que[head]]] = _next[i];
_pre[_next[i]] = _pre[que[head]];
que[ptr++] = _pre[que[head]];
};
while(que[head] <= i && head < tail)head++;
}
tail = ptr;
}
if(ans < )ans = ;
printf("%d\n", ans);
for(int i = _next[]; i <= n; i = _next[i]){
if(_next[i] != N)printf("%d ", a[i]);
}
printf("\n");
} return ;
}

HDU6215的更多相关文章

  1. Brute Force Sorting(HDU6215)

    题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...

  2. hdu6215 Brute Force Sorting

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...

  3. hdu6215 Brute Force Sorting(模拟)

    题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...

随机推荐

  1. SOPC与 hello world

    本次设计实验源码位于:http://download.csdn.net/detail/noticeable/9922865 实验目的:通过系统的搭建进一步了解FPGA的SOPC开发流程,并借此了姐pl ...

  2. 转 Master-Worker模式 并行程序设计模式--Master-Worker模式

    简介 Master-Worker模式是常用的并行设计模式.它的核心思想是,系统有两个进程协议工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负责处理子任务 ...

  3. Android studio 中的TabWidget

    1.TabWidget 的 layout文件 <?xml version="1.0" encoding="utf-8"?> <TabHost ...

  4. CentOS配置多公网

      最终目标是同一台服务器可以多个IP地址共同访问,在这个前提下又有如下两种方式: 多个公网IP使用同一个网关 多个公网IP使用不同网关   这两种方式区别所在:1.多个公网IP使用同一个网关,我们只 ...

  5. 安装VS2017后打开项目提示 asp.net 4.0尚未web服务器注册

    Visual Studio 2017 出来了,手痒安装完成后打开原来的项目缺提示,asp.net 4.0尚未web服务器注册.郁闷了… 按照提示的方法,如何:将 ASP.NET Web 应用程序升级到 ...

  6. jdk8新特性---list.stream

    项目中用到了该api ,记录下来 具有get set 构造方法的实体类 开始使用: 结果为: 更多可以参考: https://blog.csdn.net/justloveyou_/article/de ...

  7. Burpsuite常用模块详解以及渗透测试上的运用

    0x00前言 哪有什么前言,大家好,我是浅安.QQ:320229344... 0x01 JDK的安装,以及Burpsuite的成功开启. burpsuite基于JAVA环境才能正常运行的.所以要先安装 ...

  8. 第二十节:详细讲解String和StringBuffer和StringBuilder的使用

    前言 在 Java中的字符串属于对象,那么Java 中提供了 String 类来创建和操作字符串,即是使用对象:因为String类修饰的字符一旦被创建就不可改变,所以当对字符串进行修改的时候,需要使用 ...

  9. Virtualbox+Vagrant环境准备

    环境准备 所需软件 下载地址 VirtualBox-5.2.8-121009-Win https://download.virtualbox.org/virtualbox/5.2.12/Virtual ...

  10. Git工具使用

    GIT(分布式版本控制系统) Git是一款免费.开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git的读音为/gɪt/. Git是一个开源的分布式版本控制系统,可以有效.高速的处理从 ...