地址: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. expected_conditions判断页面元素

    expected_condtions提供了16种判断页面元素的方法: 1.title_is:判断当前页面的title是否完全等于预期字符串,返回布尔值 2.title_contains:判断当前页面的 ...

  2. C#------SortedLIst键值对的使用方法

    方法: SortedList sf = new SortedList(); sf.Add(, "广州"); sf.Add(, "江门"); sf.Add(, & ...

  3. Struts2_day03--课程安排_OGNL概述入门_什么是值栈_获取值栈对象_值栈内部结构

    Struts2_day03 上节内容 今天内容 OGNL概述 OGNL入门案例 什么是值栈 获取值栈对象 值栈内部结构 向值栈放数据 向值栈放对象 向值栈放list集合 从值栈获取数据 获取字符串 获 ...

  4. ajax的轮询和长轮询

    概念: 轮询(polling):客户端按规定时间定时像服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接. 概念总是枯燥的,只有代码方能解心头之快 前段代码:index.html: & ...

  5. IOS 程序内部切换语言 的一种方法

    本文转载至  http://www.cnblogs.com/wuyijibei/archive/2013/08/01/3230468.html 1: 首先, 所有的语言资源还是需要和现在的i18n方法 ...

  6. GitHub Pages站点官方宣布开始使用HTTPS

    导读 数百万人依靠GitHub Pages,将其作为他们的网站主机,除此之外,还有数百万人每天访问这些网站.为了更好地保护到GitHub Pages站点的通讯,也为了鼓励在因特网上更广泛地采用HTTP ...

  7. poj_1836 动态规划

    题目大意 N个士兵排成一排,不是按照高度顺序排列.现在想要从中去掉几名士兵,从而使得队伍中剩余的士兵能够看到这排最左边或者最右边的那个士兵,某士兵能够看到最左边(或最右边)的士兵指这名士兵和最左边(或 ...

  8. 修改\Servlet和Jsp模板的方法

    1.在MyEclipse6.6\myeclipse\eclipse\plugins下找到com.genuitec.eclipse.wizards 2.templates\替换Servlet.java ...

  9. UITextView 的 return响应事件

    在UITextView里没有UITextField里的- (BOOL)textFieldShouldReturn:(UITextField *)textField;直接的响应事件;那么在TextVie ...

  10. Fat URLs Client Identification

    w在每个URL后面都附加一个用户特有的标识码. HTTP The Definitive Guide Some web sites keep track of user identity by gene ...