HDU 6215 Brute Force Sorting(模拟链表 思维)
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
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.
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
1 2 3 4 5
0
2
1 2
2
1 3
3
2 3 5
#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(模拟链表 思维)的更多相关文章
- HDU 6215 Brute Force Sorting 模拟双端链表
一层一层删 链表模拟 最开始写的是一个一个删的 WA #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) mem ...
- 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(双向链表+队列)
题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...
- 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:Brute Force Sorting(链表+队列)
题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...
- 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 5122 K.Bro Sorting(模拟——思维题详解)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5122 Problem Description Matt's friend K.Bro is an A ...
随机推荐
- VC6完整项目代码升级到VS2010
VC6完整项目代码升级到VS2010,有需要请联系我,专业C++开发,邮箱153786575@qq.com
- JAVA多线程提高六:java5线程并发库的应用_线程池
前面我们对并发有了一定的认识,并且知道如何创建线程,创建线程主要依靠的是Thread 的类来完成的,那么有什么缺陷呢?如何解决? 一.对比new Threadnew Thread的弊端 a. 每次ne ...
- 前端观察:HTML5做得好,收入不比 iOS 差
谷歌奋进的绿色机器人超过了苹果的市场占有率,这一市场现象,导致了愈来愈多的开发者转投html5的怀抱.而HTML5将成为开发者拥抱同时拥抱Android和IOS的不二法门. 根据著名移动生态系统分析公 ...
- R2—《R in Nutshell》 读书笔记(连载)
R in Nutshell 前言 例子(nutshell包) 本书中的例子包括在nutshell的R包中,使用数据,需加载nutshell包 install.packages("nutshe ...
- Markdown tutorial [repost]
1. italic We'll start by learning two basic elements in text formatting: italics and bold. In these ...
- 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型
数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种 ...
- 免杀后门(四)之shellter注入绕过
文中提及的部分技术可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使用 Shellter 是一款动态 shellcode 注入工具.利用它,我们可以将shell注入到其他的可执行程序上,从而躲避安 ...
- flask插件系列之flask_caching缓存
前言 为了尽量减少缓存穿透,同时减少web的响应时间,我们可以针对那些需要一定时间才能获取结果的函数和那些不需要频繁更新的视图函数提供缓存服务,可以在一定的时间内直接返回结果而不是每次都需要计算或者从 ...
- geoip 扩展包根据ip定位详情
教程:https://laravel-china.org/courses/laravel-package/2024/get-the-corresponding-geo-location-informa ...
- 浅谈BeanUtils的拷贝,深度克隆
1.BeanUtil本地简单测试在项目中由于需要对某些对象进行深度拷贝然后进行持久化操作,想到了apache和spring都提供了BeanUtils的深度拷贝工具包,自己写了几个Demo做测试,定义了 ...