hdu 6215 -- Brute Force Sorting(双向链表+队列)
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.
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.
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.
题意: 有一个长为 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(双向链表+队列)的更多相关文章
- HDU 6215 Brute Force Sorting(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...
- hdu 6215 Brute Force Sorting(模拟)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...
- HDU 6215 Brute Force Sorting 模拟双端链表
一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...
- 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 ...
- hdu6215 Brute Force Sorting
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6215 题目: Brute Force Sorting Time Limit: 1000/100 ...
- HDU 6215:Brute Force Sorting(链表+队列)
题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...
- Brute Force Sorting(HDU6215)
题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1].我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成. 题解:用双 ...
随机推荐
- 传统编程和IoC的对比
ref:http://www.importnew.com/13619.html 传统编程:决定使用哪个具体的实现类的控制权在调用类本身,在编译阶段就确定了. IoC模式:调用类只依赖接口,而不依赖具体 ...
- Node.js 异步异闻录
本文首发在个人博客:http://muyunyun.cn/posts/7b9fdc87/ 提到 Node.js, 我们脑海就会浮现异步.非阻塞.单线程等关键词,进一步我们还会想到 buffer.模块机 ...
- angularui 自定义选项卡
ng-include 选取ng-template <!DOCTYPE html> <html lang="en" ng-app="myApp" ...
- 中位数的和_KEY
中位数的和 (number.pas/c/cpp) [题目描述] flower 有 N-1 个朋友,他们要一起玩一个游戏:首先确定三个非负整数 a,b,c,然后每个人依次在纸上写一个数,设第 i 个人写 ...
- iOS Storyboard约束详解
链接:http://www.jianshu.com/p/b88c65ffc3eb 约束,就是指--此处略去1万字--都懂的,就不说了.直接进入实战环节. 本文的控件约束都是围绕着UITableView ...
- oracle 行转列 列转行
行转列 这是一个Oracle的列转行函数:LISTAGG() 先看示例代码: with temp as( select 'China' nation ,'Guangzhou' city from du ...
- JPEG流封装AVI视频
前言:前几天工作任务,要把JPEG流封装为AVI视频,就找了些AVI文件结构资料和示例代码研究了下,现将学习总结及最终完成的可用代码分享出来,由于本人也是现学现用,如有不恰当或错误之处,欢迎提出! 1 ...
- PHP和JS判断变量是否定义
PHP中: 通过isset(变量名)来判断,定义返回true/未定义返回false JS中: 通过typeof来判断.
- javascript正则表达式入门
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- ctp交易平台转java接口转换
首先感谢倪材@csdn的博客,给了我很大帮助. http://blog.csdn.net/pjjing/article/details/53186394 http://blog.csdn.net/pj ...