Brute Force Sorting

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

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
【题意】给你一个序列,每次扫一遍序列,同时删除不符合排序标准的数。如果一个数符合标准,则它大于等于左边的数,小于等于右边的数,删完之后右边的数依次填补空位,求删完之后剩下的序列。
【分析】假设我们已经知道了上一次删除的数的位置,那么这一次我们要删的数肯定是上一次删除的数的前驱后缀中选,那么依次模拟链表删下去就好了,复杂度O(N).
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define mp make_pair
#define rep(i,l,r) for(int i=(l);i<=(r);++i)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = 1e5+;;
const int M = ;
const int mod = 1e9+;
const int mo=;
const double pi= acos(-1.0);
typedef pair<int,int>pii;
int n;
int a[N],pre[N],suf[N],pos[N],poss[N];
bool del[N];
int main() {
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
pre[]=;suf[n]=n;
int cnt=;
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
pre[i]=i-;
suf[i]=i+;
del[i]=false;
}
a[]=-;a[n+]=N;
for(int i=;i<=n;i++){
if(a[i]>=a[i-]&&a[i]<=a[i+])continue;
pos[++cnt]=i;
del[i]=true;
int x=suf[i],y=pre[i];
pre[x]=y;
suf[y]=x;
}
while(cnt){
int _cnt=;
for(int i=;i<=cnt;i++){
int l=pre[pos[i]];
int r=suf[l];
if(a[r]<a[l]){
if(!del[l]){
poss[++_cnt]=l;
del[l]=true;
int x=suf[l],y=pre[l];
pre[x]=y;suf[y]=suf[x];
}
if(!del[r]){
poss[++_cnt]=r;
del[r]=true;
int x=suf[r],y=pre[r];
pre[x]=y;suf[y]=x;
}
}
}
cnt=_cnt;
for(int i=cnt;i>=;i--){
pos[i]=poss[i];
}
}
int ans=;
for(int i=;i<=n;i++)if(!del[i])ans++;
printf("%d\n",ans);
for(int i=;i<=n;i++)if(!del[i])printf("%d ",a[i]);
printf("\n");
}
return ;
}

HDU 6215 Brute Force Sorting(模拟链表 思维)的更多相关文章

  1. HDU 6215 Brute Force Sorting 模拟双端链表

    一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...

  2. HDU 6215 Brute Force Sorting(链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...

  3. hdu 6215 Brute Force Sorting(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...

  4. hdu 6215 -- Brute Force Sorting(双向链表+队列)

    题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...

  5. HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...

  6. HDU 6215:Brute Force Sorting(链表+队列)

    题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...

  7. HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting

    Brute Force Sorting Time Limit: 1 Sec  Memory Limit: 128 MB 题目连接 http://acm.hdu.edu.cn/showproblem.p ...

  8. hdu6215 Brute Force Sorting

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

  9. HDU 5122 K.Bro Sorting(模拟——思维题详解)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...

随机推荐

  1. 修改tomcat的Response Hearder 头中的Server信息

    如图: Server: Apache-Coyote/1.1 这个信息给入侵者提供了一定的指示作用.为了安全起见,要求更改这个信息.那么我们就来修改一下试试,非常简单,只要在Connector中添加se ...

  2. web项目中的 log4net的配置

    最近用log4net,网上查了很多资料,照着网上的配置大多都不管用,可能我还是有什么地方配置的不对.看出来的朋友平指出.下面是我自己亲测的,可以用! 1.web项目中的web.config 配置log ...

  3. [php]数组建立方式

    1.$a[0]=..; $a[1]=..; $a[2]=..; $a[3]=..; 2.$a=array(1,2,3,4,5); 3.自定义数组 $a['logo']="qq"; ...

  4. 【CodeForces】626 F. Group Projects 动态规划

    [题目]F. Group Projects [题意]给定k和n个数字ai,要求分成若干集合使得每个集合内部极差的总和不超过k的方案数.n<=200,m<=1000,1<=ai< ...

  5. 【CF343D】 Water Tree(树链剖分)

    题目链接 树剖傻逼题,练练手好久没写树剖了. 查询忘记\(pushdown\)抓了好久虫.. 全文手写,一遍过... #include <cstdio> const int MAXN = ...

  6. 【洛谷 P3191】 [HNOI2007]紧急疏散EVACUATE(二分答案,最大流)

    题目链接 sb错误调了3hour+.. bfs预处理出每个\(.\)到每个\(D\)的最短距离. 二分时间\(t\),把每个\(D\)拆成\(t\)个点,这\(t\)个点两两连边,流量\(INF\)表 ...

  7. linux网络配置完全解析

    概述:熟悉了windows下面的网络配置,对linux下的网络配置缺未必了解透彻.熟练掌握linux下的网络配置原理,能帮助我们更容易掌握网络传输原理:同时具备一些网络连接不通对应问题的排查能力.文本 ...

  8. linux删除第几天日志【原创】

    cat del.sh #!/bin/bash thirty=`date -d '30days ago' +%Y-%m-%d` cd $ #删除输入路径下第30天的日志文件 find . -name & ...

  9. 【转载】selenium之 定位以及切换frame(iframe)

    更多关于python selenium的文章,请关注我的专栏:Python Selenium自动化测试详解 总有人看不明白,以防万一,先在开头大写加粗说明一下: frameset不用切,frame需层 ...

  10. 苹果的浏览器safari无法识别 2016-1-1这样的日期,会返回Invalid Date

    1.很多时候我们遇到的日期是2016-1-1这样的,中间是带横线的,但是有时候我们需要转化为标准的时间,即使用new Date(time)这样的方法,这时在safari浏览器里面Invalid Dat ...