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. Centos 执行top命令详细解读

    1.作用:top命令用来显示执行中的程序进程,使用权限是所有用户. 2.格式:top [-] [d delay] [q] [c] [S] [s] [i] [n] 3.主要参数: d:指定更新的间隔,以 ...

  2. [bug]”System.InvalidProgramException:公共语言运行时检测到无效程序“解决方案

    Visual Studio 2017版本15.8.x运行某些程序会报这样的错误:“System.InvalidProgramException:公共语言运行时检测到无效程序” 此问题的临时解决方案: ...

  3. 一个简单的将Markdown二级标题进行排序的脚本

    我在写博客<Linux的1000个命令>的时候,相对二级标题进行一下排序,方便阅读和查找,于是就有了这个小程序. #! /usr/bin/env python3 import os imp ...

  4. jzoj5929. 【NOIP2018模拟10.26】情书

    动态规划: #include<bits/stdc++.h> using namespace std; int n,iv[30]; #define mo 998244353 typedef ...

  5. Android 监听耳机的插拔事件

    一般采用的是动态监听的方式来实现的: package com.renhui.ej; import android.content.BroadcastReceiver; import android.c ...

  6. 《http权威指南》读书笔记1

    概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...

  7. 我自己的sublime3环境

    概述 我本来一直用的别人自带的破解版sublime3,自带插件. 前几天看<程序员修炼之道>,其中谈到了最好精通一种编辑器,我觉得说的很有道理,于是重新下了最新版的sublime3,一步步 ...

  8. redis 缓存

    本篇博客只介绍 redis 作为缓存的的一些使用,以及在项目中如何把redis和spring如何集成. 1:redis的maven依赖,redis 依赖   spring-redis 依赖: < ...

  9. Java实现大数相加、相乘(不使用BigInteger)

    大数相加: package algorithm; //使用BigInteger类验证 import java.math.BigInteger; public class BigAdd { public ...

  10. assembly.xml

    [官网地址]:http://maven.apache.org/plugins/maven-assembly-plugin/assembly.html <assembly xmlns=" ...