Brute Force Sorting

Time Limit: 1 Sec  Memory Limit: 128 MB

题目连接

http://acm.hdu.edu.cn/showproblem.php?pid=6215

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]. 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 againHelp 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  1000.
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]<=10000.0.

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 Mintegers 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

HINT

题意

有一个长度为n序列,如果第i(1<=i<=n)位上的值ai<ai-1 || ai>ai+1那么这一位需要被删除。

删除完后,再重复以上操作,直到序列单调不降。

题解:

先考虑暴力,即每次扫一遍数组,删除该删的数,直到不能删为止。

肯定有很多数扫过一遍第二次就可以不用扫了,顺着这个方向想,我们怎么节省扫描次数呢。

我们假设第一次删除了一些数,有些连着被删除的数,我将其称为一段(一个数也算一段),那么我们只需要记住每一段被删除的数的前一个数(未删除的数)即可,(想一想为什么)

将其存入一个队列,下次就直接扫这个队列即可。

具体实践,我们可以用链表记录每个位置的前一个未删除的位置以及后一个未删除的位置。

先把所有点入队,然后对于每个需要删除的点i,nex[i]肯定也要删除,则将last[i]与nex[nex[i]]相连,并将last[i]入队,当然还有一些细节需要处理,具体看代码吧。

代码:

 #include<bits/stdc++.h>
using namespace std;
#define N 1000050
int a[N],n,last[N],nex[N],f[N];
template<typename T>void read(T&x)
{
int k=;char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if(c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
} void work()
{
read(n);
nex[]=;
for(int i=;i<=n;i++)
read(a[i]),nex[i]=i+,last[i]=i-;
a[n+]=;
int sum=;
int k=,flag=;
memset(f,,sizeof(f));
queue<int> Q[];
while(!Q[k].empty())Q[k].pop();
while(!Q[-k].empty())Q[-k].pop();
for(int i=;i<=n;i++)Q[k].push(i);
while()
{
flag=;
while(!Q[k].empty())
{
int x=Q[k].front();Q[k].pop();
if (a[x]>a[nex[x]])
{
f[x]=; f[nex[x]]=;
flag=;
nex[last[x]]=nex[nex[x]];
last[nex[nex[x]]]=last[x];
last[nex[x]]=last[x];
if (f[last[x]]==&&(Q[-k].empty()||Q[-k].back()!=last[x]))Q[-k].push(last[x]);
}
}
if (flag==)break;
k=k^;
}
for(int i=;i<=n;i++) if(f[i]==)sum++;
printf("%d\n",sum);
for(int i=;i<=n;i++)
{
if(f[i]==)
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
int q;
read(q);
while(q--)
{
work();
}
return ;
}

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting的更多相关文章

  1. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  2. 2017 ACM/ICPC Asia Regional Qingdao Online 1003 The Dominator of Strings hdu 6208

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  3. 2017 ACM/ICPC Asia Regional Qingdao Online解题报告(部分)

    HDU 6206 Apple 题意: 给出四个点的坐标(每个点的坐标值小于等于1,000,000,000,000),问最后一个点是否在前三个点组成的三角形的外接圆内,是输出Accept,否输出Reje ...

  4. 2017 ACM/ICPC Asia Regional Qingdao Online 记录

    题目链接  Qingdao Problem C AC自动机还不会,暂时暴力水过. #include <bits/stdc++.h> using namespace std; #define ...

  5. hdu 6197 2017 ACM/ICPC Asia Regional Shenyang Online array array array【最长不上升子序列和最长不下降子序列】

    hdu 6197 题意:给定一个数组,问删掉k个字符后数组是否能不减或者不增,满足要求则是magic array,否则不是. 题解:队友想的思路,感觉非常棒!既然删掉k个后不增或者不减,那么就先求数组 ...

  6. HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)

    思路:找规律发现这个数是斐波那契第2*k+3项-1,数据较大矩阵快速幂搞定.   快速幂入门第一题QAQ #include <stdio.h> #include <stdlib.h& ...

  7. 2017 ACM/ICPC Asia Regional Qingdao Online Solution

    A : Apple 题意:给出三个点,以及另一个点,求最后一个点是否在三个点的外接圆里面,如果在或者在边界上,输出“Rejected”,否则输出"Accepted" 思路:先求一个 ...

  8. 2017 ACM/ICPC Asia Regional Qingdao Online - 1011 A Cubic number and A Cubic Number

    2017-09-17 17:12:11 writer:pprp 找规律,质数只有是两个相邻的立方数的差才能形成,公式就是3 * n * (n + 1) +1, 判断读入的数是不是满足 这次依然只是做了 ...

  9. 2017 ACM/ICPC Asia Regional Qingdao Online - 1008 Chinese Zodiac

    2017-09-17 13:28:04 writer:pprp 签到题:1008 Chinese Zodiac #include <iostream> #include <strin ...

随机推荐

  1. mysql utf8方式连接查看表数据乱码的问题

    起因 今天在公司第一次链接一个新的mysql数据库,我看到在spring里配置的jdbc里datasource.url里有配置characterEncoding=utf8..然后就用navicat开选 ...

  2. ssm框架整合之Spring4+SpringMVC+Mybaties3之配置文件如何配置及内容解释--可直接拷贝使用--不定时更改之2017/4/29

    经测试,需注意以下几点: 1,controller的自动扫描不能放在applicationContext.xml中,要放在spring-mvc.xml中.同样是<context:componen ...

  3. Linux下Spark框架配置(Python)

    简述  Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用并行框架,Spark,拥有Hadoop MapReduce所具有的优点:但不同于MapRedu ...

  4. Application.LoadLevel & Object.DontDestroyOnLoad

    [Application.LoadLevel] 只有在File->Build Setting中设置了的按钮才能被加载. 当level加载完成后,MonoBehaviour.OnLevelWasL ...

  5. Python基础:面向对象基础(二) 继承

    子类在继承的时候,在定义类时,小括号()中为父类的名字,父类的属性.方法,会被继承给子类,Python中允许多继承. 多继承 # 父类 Master class Master(object): def ...

  6. 压力测试工具--Siege

    Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行.s ...

  7. 刷题向》关于一道尺取法的神题目(BZOJ4653)(HARD-)(BZOJ 30题纪念)

    不得不说,这也许会是一道长期在我的博客里作为“HARD”难度存在的题 这道题能很好的考验选手的思考能力,但本蒟蒻最后还是听了省队爷讲了之后才会...(默默面壁) 题目里,说对于每一个点,是用当前选出的 ...

  8. [Plan]计划

    1. scala 2. kafka 1. lua 2. openResty 1. 日志收集 - python 2. 代码生成 3. 权限系统

  9. C#中接口声明属性,但是提示“接口”中不能有属性。

    C#中接口定义属性如下所示: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

  10. Sprig 面试中 问及 DI,IOC, AOP

    面向切面编程,把散落在程序中的公共部分提取出来,做成切面类,这样的好处在于,代码的可重用,一旦涉及到该功能的需求发生变化,只要修改该代码就行,否则,你要到处修改,如果只要修改1.2处那还可以接受,万一 ...