HDU6215
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
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
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
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
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
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的更多相关文章
- Brute Force Sorting(HDU6215)
题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
- hdu6215 Brute Force Sorting(模拟)
题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...
随机推荐
- Unix时间戳转日期时间格式,C#、Java、Python各语言实现!
之前有个Q上好友没事问我,怎么自己写Unix时间戳转日期时间?于是我就顺手写了个C#版本给他!最近想起来,就萌发多写几个语言的版本分享,权当练习思路外加熟悉另外两种语言. 先说转换步骤 先处理年份,从 ...
- struts2框架学习笔记1:搭建测试
Servlet是线程不安全的,Struts1是基于Servlet的框架 而Struts2是基于Filter的框架,解决了线程安全问题 因此Struts1和Struts2基本没有关系,只是创造者取名问题 ...
- zuul熔断代码
package com.sun.fallback; import java.io.ByteArrayInputStream; import java.io.IOException; import ja ...
- Python模块学习 - Paramiko
简介 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography). 有了Paramiko以后,我们就可以在Py ...
- python之函数参数问题(参数为可变对象)
今天看到一段代码,其中函数入参有一个参数为list,类似如下: def linux_monitor(pid=0,pidlist = []): pidlist.append(pid) 通过测试发现是有问 ...
- C# TableLayoutPanel使用方法
一.利用TableLayoutPanel类展示表格,以10行5列为例 第1步:在前台创建一个panel,使TableLayoutPanel对象填充其内部. 第2步:创建TableLayoutPanel ...
- 使用maven构建多模块项目,分块开发
在多人使用Maven协作开发项目时,尤其是稍微上点规模的项目,每个RD的工作都细分到具体功能和模块,有些模块甚至还要单独部署. 我们假设有这样一个商城项目,包括以下几个模块: 商城前台(shop) 管 ...
- rest规范是什么?
请参考这篇文章,每一个回答者侧重点不同,但都十分精彩 https://www.zhihu.com/question/28557115
- TCP/IP 笔记 - 广播和本地组播
在之前第二章介绍IP寻址的时候有介绍到,IP地址有4种:单播.组播.广播.任播. 单播,客户端与服务器之间点到点连接通信: 组播,在发送者和多个接收者(如某个特定的分组)之间实现点对多点的连接通信: ...
- AcceptEx与完成端口(IOCP)结合实例
前言 在windows平台下实现高性能网络服务器,iocp(完成端口)是唯一选择.编写网络服务器面临的问题有:1 快速接收客户端的连接.2 快速收发数据.3 快速处理数据.本文主要解决第一个问题. A ...