题目链接

题意

给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么。

思路

容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2)。想到每次删除元素的时候只会影响前后,因此考虑从前面一个位置开始检查,而不用每次都扫一遍。每次check的时候,发现了不符合题意的情况要把前面的数或者后面的数一起放进vector等待删除。然后删除vector里面把每次删除的数的前面放进一个队列,这个队列里面的值就是等待check的,而且用一个标记数组记录删除了哪些元素,这样就不用每次都扫重复的值了。复杂度接近O(n)。注意输出格式,每个数后面都有空格。

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
typedef long long LL;
int a[N], pre[N], nxt[N], vis[N];
vector<int> mark, ans;
queue<int> que; void solve() {
for(int i = 0; i < mark.size(); i++) {
int k = mark[i];
if(vis[k]) continue;
vis[k] = 1;
que.push(pre[k]);
pre[nxt[k]] = pre[k];
nxt[pre[k]] = nxt[k];
}
} void del(int i) {
if(a[pre[i]] > a[i]) {
mark.push_back(i);
mark.push_back(pre[i]);
}
if(a[nxt[i]] < a[i]) {
mark.push_back(i);
mark.push_back(nxt[i]);
}
} int main() {
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
while(!que.empty()) que.pop();
a[0] = 0; a[n+1] = 0x3f3f3f3f;
pre[n+1] = n; nxt[0] = 1;
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
vis[i] = 0;
nxt[i] = i + 1;
pre[i] = i - 1;
que.push(i);
}
while(1) {
mark.clear();
while(!que.empty()) {
int u = que.front(); que.pop();
del(u);
}
if(!mark.size()) break;
solve();
}
ans.clear();
int cnt = 0;
for(int i = 0; i <= n; i = nxt[i]) {
if(a[i] == 0) continue;
cnt++; ans.push_back(a[i]);
}
if(!cnt) puts("0\n");
else {
printf("%d\n", cnt);
for(int i = 0; i < ans.size(); i++) {
printf("%d ", ans[i]);
} puts("");
}
}
return 0;
}

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(双向链表+队列)

    题目链接 Problem Description Beerus needs to sort an array of N integers. Algorithms are not Beerus's st ...

  3. HDU 6215 Brute Force Sorting(链表)

    http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去.然后继续去 ...

  4. HDU 6215 Brute Force Sorting 模拟双端链表

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

  5. hdu 6215 Brute Force Sorting(模拟)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题解:类似双链表的模拟. #include <iostream> #include ...

  6. HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6215 题意:给你长度为n的数组,定义已经排列过的串为:相邻两项a[i],a[i+1],满足a[i]&l ...

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

  8. hdu6215 Brute Force Sorting

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

  9. hdu6215 Brute Force Sorting(模拟)

    题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...

随机推荐

  1. WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问

    原文:WPF关于控件 父级控件,子级控件,控件模板中的控件,等之间的相互访问 1,在菜单中访问 弹出菜单的控件 var mi = sender as MenuItem;//菜单条目 MenuItem ...

  2. 数据绑定(六)使用XML数据作为Binding的Source

    原文:数据绑定(六)使用XML数据作为Binding的Source .NET Framework提供了两套处理XML数据的类库 1. 符合DOM标准的类库:包括XmlDocument.XmlEleme ...

  3. vs2017 js cordova + dotnet core 开发app

    原文:vs2017 js cordova + dotnet core 开发app 1.记得在index.html加入 <meta http-equiv="Content-Securit ...

  4. C# 查农历 阴历 阳历 公历 节假日

    原文:C# 查农历 阴历 阳历 公历 节假日 using System;using System.Collections.Generic;using System.Text; namespace ca ...

  5. Git的HTTP proxy设置方法

    今天用git push代码到Github死活上不去,最后设置了Http代理才上去了,在这小记一下设置方法 1.依次打开:项目地址-->.git(可能要选择显示隐藏文件夹才能看到)-->co ...

  6. HUSTOJ的Windows版评判内核(限制内存使用)

    HUSTOJ的Windows版评判内核(一) 作者:游蓝海 个人主页:http://blog.csdn.net/you_lan_hai 2013.4.9 注:最新版本项目地址:https://gith ...

  7. 改善C#程序的建议6:在线程同步中使用信号量

    原文:改善C#程序的建议6:在线程同步中使用信号量 所谓线程同步,就是多个线程之间在某个对象上执行等待(也可理解为锁定该对象),直到该对象被解除锁定.C#中对象的类型分为引用类型和值类型.CLR在这两 ...

  8. DataTable,DataView 排序和使用

    我们都知道在Sql Server可以用order by来排序,所以很多朋友在DataTable中排序也想到了用order by关键字.但这样实现是比较困难的,下面,我们讲解一种比较简单的方法: 控制台 ...

  9. uwp汉堡菜单的实现

    ---恢复内容开始--- 现在uwp上面的汉堡菜单(就是那个三道杠,点击之后会出现菜单)使用的越来越普遍,比如微软自己家的Cortana.现在我使用的实现方法是使用SplitView实现.首先Spli ...

  10. delphi 实现微信开发(1) (使用kbmmw web server)

    原文地址:delphi 实现微信开发(1)作者:红鱼儿 大体思路: 1.用户向服务号发消息,(这里可以是个菜单项,也可以是一个关键词,如:注册会员.) 2.kbmmw web server收到消息,生 ...