HDU 6215 Brute Force Sorting(链表)
http://acm.hdu.edu.cn/showproblem.php?pid=6215
题意:
给出一个序列,对于每个数,它必须大于等于它前一个数,小于等于后一个数,如果不满足,就删去。然后继续去判断剩下的数,直到最后都满足。
思路:
建立双向链表,如果一个数是需要删除的,那么它只会影响它前一个的数和后一个的数,这样只需要把它前面的数保存下来,下次再跑即可。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn = +; int n ,top;
int a[maxn],pre[maxn],nxt[maxn];
int que[maxn]; template <class T>
inline void scan_d(T &ret)
{
char c;
ret = ;
while ((c = getchar()) < '' || c > '');
while (c >= '' && c <= '')
{
ret = ret * + (c - ''), c = getchar();
}
} int main()
{
//freopen("in.txt","r",stdin);
int T;
scan_d(T);
while(T--)
{
top=;
scan_d(n);
for(int i=;i<=n;i++)
{
scan_d(a[i]);
pre[i]=i-;
nxt[i]=i+;
que[top++]=i;
}
a[]=,a[n+]=INF;
nxt[]=,pre[n+]=n;
bool flag = true;
int ans = n;
while(flag)
{
int s = ;
flag = false;
int now = ;
while(now<top)
{
int cnt = ;
int it = que[now];
while(a[it]>a[nxt[it]]) {cnt++;it=nxt[it];flag=true;}
if(cnt)
{
ans-=(cnt+);
nxt[pre[que[now]]]=nxt[it];
pre[nxt[it]]=pre[que[now]];
que[s++]=pre[que[now]];;
}
while(que[now]<=it && now<top) now++;
}
top=s;
}
printf("%d\n",ans);
for(int i=;nxt[i]!=n+;i=nxt[i])
printf("%d ",a[nxt[i]]);
printf("\n");
}
return ;
}
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 模拟双端链表
一层一层删 链表模拟 最开始写的是一个一个删的 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 -- 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 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 6215:Brute Force Sorting(链表+队列)
题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...
- hdu6215 Brute Force Sorting(模拟)
题意 给一个长度为n(n<=1e5)的序列,如果一个位置i满足a[i-1]>a[i]或者a[i]>a[i+1],那么我们就称该位置是不合法的位置 先把序列中所有不合法的位置统一找出来 ...
随机推荐
- mybatis源码解析5---SqlSession解析
由之前解析可知,mybatis启动的时候会加载XML配置文件解析生成全局配置对象Configuration对象,SqlSessionFactoryBuilder类会根据Configuration对象创 ...
- git使用,多分支合并代码解决冲突,git删除远程分支,删除远程master默认分支方法
git使用,多分支合并代码解决冲突,git删除远程分支,删除远程master默认分支方法提交代码流程:1.先提交代码到自己分支上2.切换到devlop拉取代码合并到当前分支3.合并后有变动的推送到自己 ...
- XWIKI部署安装
http://www.linuxidc.com/Linux/2016-08/134408.htm
- 洛谷 P1106 删数问题
一定要认真审题 “去掉其中任意k个数字后剩下的数字按原左右次序将组成一个新的正整数” 也就是说 输入:7893400 4 输出:300 (00在原顺序中位于3后边) 输入:789 ...
- 一位前辈的博客,收获颇丰,包括Android、Java、linux、前端、大数据、网络安全等等
https://www.cnblogs.com/lr393993507/ 魔流剑
- 怎样从外网访问内网SQLServer数据库?
本地安装了一个SQLServer数据库,只能在局域网内访问到,怎样从外网也能访问到本地的SQLServer数据库呢?本文将介绍具体的实现步骤. 1. 准备工作 1.1 安装并启动SQLServer数据 ...
- DOS下读取spd信息的汇编程序(通过SMBus)
汇编程序编写的读取spd信息的代码: ;----------------------------------------------------------- ;功能: 通过SMbus 读取内存的SP ...
- linux 安装sqlite3
python2个版本导致的问题. 网上找了好多方法都不行. 最后自己莫名其妙弄好了, 回想了一下大概是 安装sqlite3 重新安装python 最后 yum update 更新 就好了.
- 给PXC集群加密
MySQL的复制时明文的,不管是集群的复制还是IST/SST,直接通过抓包就可以抓取数据. 生成证书 直接使用 mysql_ssl_rsa_setup mysql_ssl_rsa_setup --da ...
- 配置使用 git 秘钥连接 GitHub
配置使用 git 秘钥连接 GitHub 在Linux下部署Git环境 1.安装Git. 使用命令安装 git . sudo apt-get install git 2.创建一个 Github 账号 ...