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

题目:

Brute Force Sorting

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

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
 
思路:
  直接模拟多次扫数组肯定不行,TLE。
  仔细观察后发现在删一个数后v[i],在下一次的扫描中只会影响v[i]的前一个数和后一个数,所以我们可以用双向链表做。
  每次把被影响的数保存起来,然后从这些数开始扫,用链表模拟删除操作,把删除数的前一个数留到下次扫描。
  因为被删除的数最多只有n个,所以时间复杂度O(n)。
  我因为用了set来去重,时间复杂度变成了O(nlogn),不过也可以不用set,我只是为了好写。
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; int now,pre,v[K],pe[K],nt[K],dl[K];
set<int>st;
vector<int>tmp; int main(void)
{
//freopen("in.acm","r",stdin);
int t,n;cin>>t;
while(t--)
{
memset(dl,,sizeof dl);
scanf("%d",&n);
for(int i=;i<=n;i++) dl[i]=,scanf("%d",v+i),pe[i]=i-,nt[i]=i+,st.insert(i);
nt[]=,pe[n+]=n,pe[]=,nt[n]=n+;
v[]=,v[n+]=K;
while(st.size())
{
tmp.clear();
for(auto &x:st)
{
int ntx=nt[x],px=pe[x];
if(v[px]>v[x]) tmp.PB(px),tmp.PB(x);
if(v[x]>v[ntx]) tmp.PB(x),tmp.PB(ntx);
}
st.clear();
for(auto &x:tmp)
if(!dl[x])
{
int ntx=nt[x],px=pe[x];
nt[px]=ntx,pe[ntx]=px;
st.insert(px);
dl[x]=;
}
}
int cnt=;
for(int i=nt[];i!=n+;i=nt[i]) cnt++;
printf("%d\n",cnt);
for(int i=nt[];i!=n+;i=nt[i]) printf("%d ",v[i]);
printf("\n");
}
return ;
}

hdu6215 Brute Force Sorting的更多相关文章

  1. hdu6215 Brute Force Sorting(模拟)

    题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...

  2. HDU 6215 Brute Force Sorting(模拟链表 思维)

    Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  3. 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 ...

  4. Brute Force Sorting(HDU6215)

    题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...

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

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

  6. HDU 6215 Brute Force Sorting(链表)

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

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

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

  8. hdu 6215 Brute Force Sorting(模拟)

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

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

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

随机推荐

  1. hdu 2527:Safe Or Unsafe(数据结构,哈夫曼树,求WPL)

    Safe Or Unsafe Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. VC++ 轻松实现“闪屏” SplashWnd

    我们平时使用的好多软件在运行启动时都会有一个“闪屏”画面显示,一般用于标识软件的一些信息,如软件版本名称.公司等,通过查找资料发现,其实实现起来很简单,一个类就能搞定! SplashWnd.h  C+ ...

  3. Java各种日期格式的获取和设置指定日期

    因为近期在做一个项目,发现项目中日期设置的bug,于是查阅了多方资料后.最终攻克了,为此写篇总结.方便日后的查阅. 多的不说了.直接上代码 package com.example.testdate; ...

  4. php如何互换一个数组的首尾元素 中间不变 首尾互换

    群里有人提出一个问题 如何互换一个数组的首尾元素 中间不变 首尾互换 代码如下: <?php $array=array(1,2,3,4,5,6,7,8,9,10); $first=array_s ...

  5. 工作中php处理HTTP请求的缺陷总结

    之前遇到过php在处理一部分业务的时候总是感觉有短板,有些东西总是不能随人心意.比如执行时间问题,一个进程需要读取多条数据放入数组循环执行处理流程,就会很慢,容易超时,这个超时问题 可以用php设置s ...

  6. java基础---->Java中异常的使用(二)

    这一篇博客用例子讲述一下异常的处理过程.那些 我们一直惴惴不安 又充满好奇的未来 会在心里隐隐约约地觉得它们是明亮的. 异常的执行过程 一.实例一:return语句 public class Exce ...

  7. 【BZOJ1495】[NOI2006]网络收费 暴力+DP

    [BZOJ1495][NOI2006]网络收费 Description 网络已经成为当今世界不可或缺的一部分.每天都有数以亿计的人使用网络进行学习.科研.娱乐等活动.然而,不可忽视的一点就是网络本身有 ...

  8. 任务调度quartz整理

    一张图,了解quartz运行机制: 此图表示:Scheduler是容器,Trigger是多个触发器,jobDetail是多个任务,Calendar是多个日历. jobDetail任务,需要指定类实现J ...

  9. vim简单的配置

    "设置当文件被改动时自动载入 set autoread "在左侧行号 set nu "tab长度为4 "覆盖文件时不备份 set nobackup " ...

  10. 微软构建高效DevOps团队培训总结

    9.21和9.22这两天参加了微软DevOps的培训,主要是围绕TFS2015的不少新功能来讲的,相比较之前我们一直使用TFS2013来管理团队,确实强大了不少,也更加实用了. 首先,什么是DevOp ...