Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列
2015-06-30 10:18
465 查看
B. Psychos in a Line
Time Limit: 20 SecMemory Limit: 256 MB
题目连接
http://codeforces.com/problemset/problem/319/BDescription
There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who has an id greater than the psycho to his right (if exists) kills his right neighbor in the line. Note that a psycho might kill and get killed at the same step.You're given the initial arrangement of the psychos in the line. Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment. Look notes to understand the statement more precise.
[b]Input[/b]
The first line of input contains integer n denoting the number of psychos, (1 ≤ n ≤ 105). In the second line there will be a list of n space separated distinct integers each in range 1 to n, inclusive — ids of the psychos in the line from left to right.
[b]Output[/b]
Print the number of steps, so that the line remains the same afterward..
[b]Sample Input[/b]
10
10 9 7 8 6 5 3 4 2 1
[b]Sample Output[/b]
2
HINT
[b]题意[/b]
给你一堆数,每个数次都可以吃掉他右边连续减小的序列,问你多少轮之后,会变成上升序列
[b]题解:[/b]
一开始想的暴力,用并查集/链表优化一下,结果不幸被t(其实也是情理之中……
T的数据是 100000 1 2 3 4 5…… 99999
正解是单调队列,维护一个单调下降的序列,就可以找到一直杀到哪儿的位置。
f[i]表示杀人的次数
[b]代码[/b]
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 1000005 #define mod 10007 #define eps 1e-5 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** int a[maxn]; int f[maxn]; int t[maxn]; int main() { int n=read(),top=0,ans=0; for(int i=0;i<n;i++) a[i]=read(); for(int i=n-1;i>=0;i--) { int tt=0; while(top&&a[t[top-1]]<a[i]) f[i]=tt=max(tt+1,f[t[--top]]); t[top++]=i; } printf("%d",*max_element(f,f+n)); }
相关文章推荐
- Linux中断(interrupt)子系统之四:驱动程序接口层 & 中断通用逻辑层
- CSS常用属性
- Python中用format函数格式化字符串的用法
- jquery鼠标随意移动div
- jsp页面乱码问题的解决
- php 构造方法
- matrix-gui-browser-2.0 matrix-browser Qt QWebView hacking
- .wang域名市场报告:前十四强均为中国域名商(6月29日)
- VNC连接VMware vSphere ESXi5.5
- 欢迎使用CSDN-markdown编辑器
- 关于如何获得自己项目启动到手机顶部Activity的实时对象
- Axure7.0的中继器(Repeater)
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你
- [Asp.net]常见word,excel,ppt,pdf在线预览方案,有图有真相,总有一款适合你
- 要学习TINY框架要有什么前提条件?
- 2015.6.29
- Android 网络通信框架Volley简介(Google IO 2013)
- c问题
- UNIX环境高级编程(第17章 高级进程间通信)
- dyld: Library not loaded: @rpath/XCTest.framework/XCTest Referenced from: /private/var/mobile/Conta