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 ...
随机推荐
- 5W次单点修改,求最长的连续上升子序列 HDU 3308
题目大意:给你n个数,m个操作. 有两种操作: 1.U x y 将数组第x位变为y 2. Q x y 问数组第x位到第y位连续最长子序列的长度. 对于每次询问,输出连续最长子序列的长度 思路:用线段树 ...
- ZOJ 3781 Paint the Grid Reloaded 连通块
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3781 题意:n*m只由OX组成的矩阵,可以选择某一连通块变成另一 ...
- 将oh-my-zsh编程真正的my zsh
环境: Ubuntu 32位 oh-my-zsh安装: 1.安装zsh: sudo apt-get install zsh 2.将当前用户的shell环境修改为zsh: chsh -s /bin/z ...
- 【BZOJ】1143: [CTSC2008]祭祀river
[题意]求DAG上最多的点使得互不可达. [算法]floyd+最大匹配 [题解] 链是DAG上的一个点集,集合内的点相互单向可达. 反链是DAG上的一个点集,集合内的点相互不可达. 题目显然是求最长反 ...
- js中不能做变量名的字符
JavaScript中不能作为变量名的关键字和保留字总结: 1.js中的关键字: break case catch continue default delete do else finally fo ...
- HTML5实现仪表盘、温度计等插件实用源码
- styled-components真的好吗?
最近在学习react,然后遇到react中css该怎么写这个问题,上知乎上看了好多大牛都说styled-components好用是大势所趋. 但我自己用了感觉体验却很差,我在这里说说我为啥觉得styl ...
- .net APIHelper client获取数据
using Newtonsoft.Json; using System.Net.Http.Headers; public static class APIHepler { public static ...
- layui结合mybatis的pagehelper插件的分页通用的方法
总体思路: 1.前台查询的时候将当前页和页大小传到后台 2.后台将当前页,页大小以及数据与数据总数返回前台,前台显示完表格完数据之后显示分页插件. 前台页面: 准备查询条件的表单,与数据表格,分页di ...
- perl6 登录phpmyadmin
use HTTP::UserAgent; my $ua = HTTP::UserAgent.new; my $url = 'http://localhost/phpMyAdmin/index.php' ...