题目链接

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
 
 

题意: 有一个长为 n 的数列a[1]~a[n],现在对这个数列进行删除操作,如果 a[i]>a[i+1] 则删去 a[i] 和 a[i+1],进行完一轮之后如果还有不满足的数,则进行第二轮删除操作,求最后剩下的数的个数,并且输出这些数?

思路: 使用双向链表进行模拟,对于每一个节点记录其左边的节点和右边节点的下标,首先把1~n的下标都放入队列中,每次从队列中取出一个下标now , pre=a[now].l , next=a[now].r , 判断a[now].x<=a[next].x , 如果满足则表示合理,从队列中取下一个数;否则将 pre 放入队列,因为现在要模拟删除 now 和 next ,删除之后可能pre和下一个节点大小关系就不合理了,所以需要放入队列中,另外还要进行

a[pre].r=a[next].r;
         a[a[next].r].l=pre;
         a[next].l=pre;

前两个很明显表示删除now和next,最后一个a[next].l=pre,是因为可能next也在队列中,下一个要判断是否删除 next 和 a[next].r , 删除之后都需要将链表前后连接起来,例如对于n=5 , 2 5 3 1 4这样的数据,删除5和3数值后(对应的下表为2和3,下标从1开始),下一个3和1也不满足。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int N=1e5+;
struct Node
{
int l,r;
int x;
}a[N];
queue<int>Q; int main()
{
int T; cin>>T;
while(T--)
{
int n; scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i].x);
a[i].l=i-;
a[i].r=i+;
Q.push(i);
}
a[].r=; a[n+].x=;
while(!Q.empty())
{
int now=Q.front(); Q.pop();
int pre=a[now].l;
int next=a[now].r;
if(a[now].x>a[next].x)
{
Q.push(pre);
a[pre].r=a[next].r;
a[a[next].r].l=pre;
a[next].l=pre;
}
}
int ans=;
int now=a[].r;
while(now<=n)
{
ans++;
now=a[now].r;
}
printf("%d\n",ans);
now=a[].r;
while(now<=n)
{
printf("%d ",a[now].x);
now=a[now].r;
}
puts("");
}
return ;
}

hdu 6215 -- Brute Force Sorting(双向链表+队列)的更多相关文章

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

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

  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 模拟双端链表

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

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

  7. hdu6215 Brute Force Sorting

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

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

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

  9. Brute Force Sorting(HDU6215)

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

随机推荐

  1. appium实例编写(1)---以ContactsTest.apk 操作为例

    详情参照   http://www.cnblogs.com/puresoul/p/4696825.html#3326873   自己练习一遍 前言: appium环境搭建参照另一篇博客:http:// ...

  2. Java通过链表实现队列

    class LinkedQueue<T> { /** * 队列大小,由构造函数初始化 */ private int maxSize; /** * 队头 */ private Node fr ...

  3. jQuery中的常用内容总结(一)

    jQuery中的常用内容总结(一)   前言 不好意思(✿◠‿◠),由于回家看病以及处理一些其它事情耽搁了,不然这篇博客本该上上周或者上周写的:同时闲谈几句:在这里建议各位开发的童鞋,如果有疾病尽快治 ...

  4. 云计算之阿里仓库停止openstack mitaka源报错“No package centos-release-openstack-mitaka available.”

    之前学习了一个月的openstack的mitaka版本,写完脚本放置一段时间,最近准备正式部署突然发现 No package centos-release-openstack-mitaka avail ...

  5. MapReduce极简教程

    一个有趣的例子 你想数出一摞牌中有多少张黑桃.直观方式是一张一张检查并且数出有多少张是黑桃?   MapReduce方法则是: 给在座的所有玩家中分配这摞牌 让每个玩家数自己手中的牌有几张是黑桃,然后 ...

  6. 用vue开发一个app(3,三天的成果)

    前言 一个vue的demo 源码说明 项目目录说明 . |-- config // 项目开发环境配置 | |-- index.js // 项目打包部署配置 |-- src // 源码目录 | |-- ...

  7. PYTHON 函数局部变量和全局变量

    有这样一段PYTHON代码,从事C语言开发的人都知道,如果定义了全局变量,而函数内没有定义同名的函数变量的话,那么在函数内对该变量的赋值就是对全局变量空间数值的修改, 然后在PYTHON中却不尽相同, ...

  8. C++11获取线程的返回值

    C++11 std::future and std::promise 在许多时候,我们会有这样的需求--即我们想要得到线程返回的值. 但是在C++11 多线程中我们注意到,std::thread对象会 ...

  9. babel的使用详解

    由于es6的很多特性在旧的浏览器下支持不好,我们在使用的时候需要将其转化为es5,下面介绍babel解析器的使用 一:独立使用babel的方法 1.本地安装babel-cli npm install ...

  10. Spark官方1 ---------Spark SQL和DataFrame指南(1.5.0)

    概述 Spark SQL是用于结构化数据处理的Spark模块.它提供了一个称为DataFrames的编程抽象,也可以作为分布式SQL查询引擎. Spark SQL也可用于从现有的Hive安装中读取数据 ...