hdu 6215 Brute Force Sorting 【链表+队列】
来源:互联网 发布:阿里云搭建个人网站 编辑:程序博客网 时间:2024/06/06 03:05
题意:
给你一个数列,每次删除所有a[i]. a[i]满足, a[i]<=a[i-1] || a[i]>=a[i+1],
从而获得一个新数列,如果新数列还有这样的数,重复以上操作。
直到数列成为单调递增为止。
题解:
很容易想到链表模拟,但是直接一遍一遍扫肯定tle
然后可以发现,每次删掉一个数之后,受影响的只有他之前和他之后的两个数。
所以,我们可以用两个队列,一个保存当前剩余的数有哪些,另一个保存,上次删除的数的上一个数是那些:
#include<bits/stdc++.h>using namespace std;#define ll long longconst int maxn=1e5+10;const int eps=1e-4;int n,x,cnt;int pre[maxn],nxt[maxn],num[maxn],ans[maxn];queue<int>que,tque;void init(){ cnt=0; while(!que.empty()) que.pop(); scanf("%d",&n); for(int i=1;i<=n;++i){ scanf("%d",&x); num[++cnt]=x; pre[cnt]=cnt-1; nxt[cnt]=cnt+1; que.push(i); } num[0]=0;nxt[0]=1; num[++cnt]=maxn; pre[cnt]=cnt-1; nxt[cnt]=cnt+1;}int main(){ int T; scanf("%d",&T); while(T--){ init(); while(1){ int t,y,z,f; while(!tque.empty()) tque.pop(); while(!que.empty()){ t=que.front(); que.pop(); y=nxt[t]; z=num[t]; f=0; while(y!=cnt&&z>num[y]){ z=num[y]; y=nxt[y]; f=1; } if(f){ pre[y]=pre[t]; nxt[pre[t]]=y; tque.push(pre[t]); while(!que.empty()&&que.front()<y) que.pop(); } } if(tque.size()==0) break; while(!tque.empty()){ que.push(tque.front()); tque.pop(); } } int s=0,p=0; while(s!=cnt){ if(s!=0) ans[++p]=num[s]; s=nxt[s]; } printf("%d\n",p); for(int i=1;i<=p;++i) printf("%d ",ans[i]); puts(""); } return 0;}
阅读全文
0 0
- hdu 6215 Brute Force Sorting 【链表+队列】
- HDU 6215 Brute Force Sorting 链表 + 队列(模拟)
- HDU 6215 Brute Force Sorting [链表]
- HDU 6215 Brute Force Sorting [链表]
- Hdu 6215 Brute Force Sorting【链表】
- HDU 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting
- HDU 6215 Brute Force Sorting 模拟
- HDU 6215 Brute Force Sorting【双端链表】
- HDU 6215 2017Brute Force Sorting 青岛网络赛 队列加链表模拟
- 【2017青岛网络赛】1010 hdu 6215 Brute Force Sorting 双向链表+队列
- hdoj 6215 Brute Force Sorting
- hdu 6215 Brute Force Sorting 模拟链表
- HDU 6215 Brute Force Sorting (链表维护数组)
- HDU 6215 Brute Force Sorting(伪双向链表删边)
- HDU-6215 Brute Force Sorting(思维、模拟链表)
- HDU6215 Brute Force Sorting 【模拟】
- java juc2
- xml读取properties配置文件,web.xml读取properties,properties格式,properties在eclipse创建
- LoadCursor 加载不同的鼠标光标
- GitHub按钮功能
- Jquery的html方法里包含特殊字符的处理,类似双引号之类的
- hdu 6215 Brute Force Sorting 【链表+队列】
- PG update操作说明
- 如何定位虚拟机内部附加的数据磁盘
- 网络编程之编写LSP进行Winsock API监控拦截或LSP注入
- C语言main函数
- 深度学习面经
- shell命令-awk常用命令
- pg_ctl stop -m fast停止时的异常后台日志
- 【基础算法】石子合并-版本1