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(链表)的更多相关文章

  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 模拟双端链表

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

  3. hdu 6215 Brute Force Sorting(模拟)

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

  4. hdu 6215 -- Brute Force Sorting(双向链表+队列)

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

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

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

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

  7. hdu6215 Brute Force Sorting

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

  8. HDU 6215:Brute Force Sorting(链表+队列)

    题目链接 题意 给出一个长度为n的数组,每次操作都要删除数组里面非递增的元素,问最终的数组元素有什么. 思路 容易想到用链表模拟删除,但是不能每次都暴力枚举,这样复杂度O(N^2).想到每次删除元素的 ...

  9. hdu6215 Brute Force Sorting(模拟)

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

随机推荐

  1. 【转】机器学习笔记之(3)——Logistic回归(逻辑斯蒂回归)

    原文链接:https://blog.csdn.net/gwplovekimi/article/details/80288964 本博文为逻辑斯特回归的学习笔记.由于仅仅是学习笔记,水平有限,还望广大读 ...

  2. C# 调整控件的Z顺序

    当窗口或者容器控件中的控件在布局过程中发生重叠的时候,会出现层次性.Z顺序较大的控件会遮挡Z顺序较小的控件,放在顶层的控件会挡住放在底层的控件. 1.编辑一个这样的窗口(使用Label控件) 2.添加 ...

  3. 删去k个数字后的最小值

    public static String removeKDigits(String num,int k) { //新整数的最终长度=原长度 - k int newLength=num.length() ...

  4. MySQL笔记(八)存储过程练习&补充

    存储过程有什么优缺点?为什么要用存储过程?或者在什么情况下才用存储过程? 最直白的好处是存储过程比较快. 1.利用存储过程,给Employee表添加一条业务部门员工的信息. DROP PROCEDUR ...

  5. php开启xdebug扩展

    1.下载Xdebug(先看php下的ext文件夹(C:\xampp\php\ext)下有没有php_xdebug.dll文件,如果有的话,就不用下了.) 到目前为止,Xdebug的最新版本为2.7.0 ...

  6. Pytorch-学习记录 卷积操作 cnn output_channel, etc.

    参考资料: pytorch中文文档 http://pytorch-cn.readthedocs.io/zh/latest/

  7. redis-3.2.7安装脚本

    redis-3.2.7 安装脚本 for CentOS 6.5 #!/bin/bash #Filename: inst_for_redis.sh #Version: 1.1 #Lastdate: 20 ...

  8. mariadb的flashback到底怎么样???防误删可以,但算不上真正的闪回--再看mariadb 10.3的System-Versioned Tables

    mariadb 在10.2.4引入闪回特性,支持DML(INSERT, DELETE, UPDATE)操作的闪回,不支持DDL语句,使用闪回,必须设置binlog_row_image=FULL. 其原 ...

  9. 【题解】luogu P3386 【模板】二分图匹配

    题面:https://www.luogu.org/problemnew/show/P3386 好像没有人发Ford-Fulkerson,我来一发, 这道题和P2756飞行员配对方案问题方法一样,网络流 ...

  10. Iris Classification on Tensorflow

    Iris Classification on Tensorflow Neural Network formula derivation \[ \begin{align} a & = x \cd ...