HDU 6215:Brute Force Sorting(链表+队列)
题意
给出一个长度为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(链表+队列)的更多相关文章
- 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(双向链表+队列)
题目链接 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 模拟双端链表
一层一层删 链表模拟 最开始写的是一个一个删的 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 题解:类似双链表的模拟. #include <iostream> #include ...
- 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 ...
- hdu6215 Brute Force Sorting(模拟)
题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...
随机推荐
- WPF与缓动(三) 指数缓动
原文:WPF与缓动(三) 指数缓动 WPF与缓动(三) 指数缓动 ...
- MySql 5.7 重置root密码
一.以安全模式登录 # Stop MySQL sudo service mysql stop # Make MySQL service directory. sudo mkdir -p /var/ru ...
- ASP .NET Model
Model是全局变量,一个页面一个 前台 @ModelWebApplication1.Models.Movie; @{ ViewBag.Title = "ModelTest"; } ...
- C#调用Resources.resx资源文件中的资源
使用到了.NET中的资源文件,也就是Resources.resx,于是就学会了如何调用资源文件中的资源.首先,资源文件可以从项目属性中的资源标签添加.比如,我添加一个图片,叫做aaa.png,添加入资 ...
- JS 小鸟飞
<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- js 点击超链接,执行js脚本,而不进行url跳转
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta ...
- MVC 自定义路由规则
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mv ...
- C#热敏打印图片 串口打印图片
原文:C#热敏打印图片 串口打印图片 如图,一步一步慢慢调出来的 //串口通信类 public System.IO.Ports.SerialPort serialPort = null; serial ...
- 树莓派3B 无显示器,无键盘,无Linux系统,无网线 配置WIFI连接
#1.基本需求#2.烧写镜像#3.用有线网(网线)连接PC,实现远程操作树莓派#4.接入无线网,通过其它电脑远程控制树莓派#5.使用PC共享的热点Wifi远程控制树莓派 #1.基本需求 树莓派 USB ...
- Win10中解决Prolific PL2303出现错误代码10的问题
PL2303 是Prolific 公司生产的一种高度集成的RS232-USB接口转换器,在Win10中默认安装的驱动程序会出现错误代码10的问题,如下图所示: 下载Win10上可以用的PL2303驱动 ...