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. 如何读取R 的sumary()结果

    思路 step 1: sum = summary(model) step 2: sum有好多属性,直接根据属性名称引用($)即可, 如: + > sum$call 返回 model 使用的模型语 ...

  2. Dev修改gridview 背景色

    private void gridView1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventA ...

  3. Python大法之告别脚本小子系列—信息资产收集类脚本编写(下)

    作者:阿甫哥哥 原文来自:https://bbs.ichunqiu.com/article-1618-1.html 系列文章专辑:Python大法之告别脚本小子系列目录: 0×05 高精度字典生成脚本 ...

  4. centoos内核升级

    1.检查当前CentOS内核版本 uname -r 2.导入key 打开http://elrepo.org/tiki/tiki-index.php 复制执行该命令 3.安装ELRepo 打开2步中的网 ...

  5. Android开发工程师文集-提示框,菜单,数据存储,组件篇

    提示框,菜单,数据存储,组件篇 Toast Toast.makeText(context, text, 时间).show(); setDuration();//设置时间 setGravity();// ...

  6. [Swift]LaunchScreen.storyboard设置启动页

    设置[General]->[App Icons and Launch Images] 注意:当App不是用LaunchScreen.xib来配置启动页时,[LaunchScreen File]置 ...

  7. LVS-1

    lvs 再调度上非常出色,能处理400万的并发: 在功能上不足,需要其他软件的支持.

  8. HTML里面form表单name,action,method,target,enctype等属性用法

    HTML里面的form表单里面的name,target,enctype,method以及action的用法 HML表单HTML里面的表单是HTML页面与浏览器交互的重要手段,表单主要提交一些客户端的数 ...

  9. javaScript 二分查找

    什么是二分查找的,举个栗子: var arr = [1, 3, 5, 7, 9, 11, 14, 15, 17, 19, 20]; 上面有序数组, 随便给你一位 9 ,输出该数在数组中的索引.   当 ...

  10. 记CTC原理

    CTC,Connectionist temporal classification.从字面上理解它是用来解决时序类数据的分类问题.语音识别端到端解决方案中应用的技术.主要是解决以下两个问题 解决语音输 ...