Brute Force Sorting(HDU6215)
题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]<=a[i+1]。我们每次对当前数组删除非排序过的串,合并剩下的串,继续删,直到排序完成。
题解:用双向链表模拟删除过程即可。
具体细节见代码:
#include<cstdio>
#include<cstring>
#define N 200010
using namespace std;
int T,n,i,a[N],out[N],next[N],pre[N],r,pos[N],l;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
l=r=;
memset(pos,,sizeof(pos));
memset(out,,sizeof(out));
memset(next,,sizeof(next));
memset(pre,,sizeof(pre));
for(i=;i<=n;i++){
scanf("%d",&a[i]);
next[i]=i+;
pre[i]=i-;
if(a[i]<a[i-]){///标记第一次遍历所删除的数
if(!out[i-])pos[++r]=i-,out[i-]=;///r表示所删除的数的数量
pos[++r]=i;out[i]=;
}
}
for(l=;l<=r;){
int rr=r;
for(i=l;i<=rr;i++){
next[pre[pos[i]]]=next[pos[i]];
pre[next[pos[i]]]=pre[pos[i]];///将pos[i]所表示的数删去
if(pre[pos[i]]==||next[pos[i]]==n+)continue;///遍历完毕
if(!out[next[pos[i]]]&&a[next[pos[i]]]<a[pre[pos[i]]]){///如果已经删除的数后面一个数并未被删除,且这一个数小于已经删除的数的前一个数,那么则删除它
if(!out[pre[pos[i]]])pos[++r]=pre[pos[i]],out[pre[pos[i]]]=;
pos[++r]=next[pos[i]];out[next[pos[i]]]=;
}
}
l=rr+;
}
printf("%d\n",n-r);
for(i=;i<=n;i++)
if(!out[i])printf("%d ",a[i]);puts("");
}
}
Brute Force Sorting(HDU6215)的更多相关文章
- 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(模拟链表 思维)
Brute Force Sorting Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Othe ...
- 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(模拟)
题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...
- hdu 6215 -- Brute Force Sorting(双向链表+队列)
题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...
- HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...
- HDU 6215:Brute Force Sorting(链表+队列)
题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...
- 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 ...
随机推荐
- map的key 为指针
STL中map的key能否用char *呢?当然可以! 在程序中需要用到一个map,本来是这样写的,map<string, int> mapStr; 为了追求效率,把string改成了ch ...
- [x] 封装、继承,多态
那么多态的作用是什么呢,封装可以使得代码模块化,继承可以扩展已存在的代码,他们的目的都是为了代码重用. 而多态的目的则是为了接口重用.也就是说,不论传递过来的究竟是那个类的对象,函数都能够通过同一个接 ...
- 使用RxSwift 实现登录页面的条件绑定
我们在使用MVC建构进行开发时,对登录页面用户名密码等进行的处理一般是这样的,点击登录按钮判断用户框以及密码框输入的合法性,用一堆if真是屎一般!或者用textfield的代理来进行响应其实也是屎一般 ...
- java中方法内可以调用同一个类中的方法
在同一个类中,java的普通方法的相互调用,可以使用this+点号+方法名,也可省略this+点号,java编 译器会自动补上.
- 准备下上机考试,各种排序!!以后再添加和仿真像wiki上那样!
#include <stdio.h> #include <string.h> #define N 6 typedef struct { ]; int score; }stude ...
- table-cell 布局
table-cell能实现段落文字相对于div的垂直居中: 将div设置为display:table-cell; *display:inline-block;text-align:center; ve ...
- BBbacktrace installation
1: get the installation package https://backtrackbb.readthedocs.io/en/latest/Method.html#overview ht ...
- HTTPS加密原理(转)
Header HTTP.HTTPS在我们日常开发中是经常会接触到的. 我们也都知道,一般 Android 应用开发,在请求 API 网络接口的时候,很多使用的都是 HTTP 协议:使用浏览器打开网页, ...
- C++四种类型之间的转换
C风格的强制类型转换(Type Cast)很简单,不管什么类型的转换统统是: TYPE b = (TYPE)a. C++风格的类型转换提供了4种类型转换操作符来应对不同场合的应用. const_cas ...
- pytest 的 yield
前言:1.当 pytest.fixture(scope="module") 时,pytest的yieId 类似unittest的teartownclass 2.当 pytest.f ...