Psychos in a Line

CodeForces - 319B

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.

Input

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.

Output

Print the number of steps, so that the line remains the same afterward.

Examples

Input

1010 9 7 8 6 5 3 4 2 1

Output

2

Input

61 2 3 4 5 6

Output

0

Note

In the first sample line of the psychos transforms as follows: [10 9 7 8 6 5 3 4 2 1]  →  [10 8 4]  →  [10]. So, there are two steps.

题意:

 给定一个n个数的全排列,每一次操作,一个数字会消失,当且仅当它左边有数字,并且比它大。 问几次操作后数组将稳定不变。

思路:

用单调栈解决本问题,

我们栈中维护两个信息,一个是数字的大小,另一个维护这个数字会被第几次消灭,记为cnt。

以数字的大小维护严格递减的单调栈。

那么如果栈是空的,这个数设置成第0次被消灭,因为他永远不会被消灭,一直存在,所以设为0.

否则 如果栈顶元素小于当前元素,将其弹出,同时维护当前元素弹出的所有元素中,被第几次被消灭的最大值m。

那么加入这个元素时,cnt就为 m+1

如果没有弹出元素,就设置为1.

那么答案就是加入到栈中的所有cnt中的最大值。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
int a[maxn];
stack<pii> st; int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gbtb;
cin>>n;
repd(i,1,n)
{
cin>>a[i];
}
int ans=0;
repd(i,1,n)
{
if(st.empty())
{
st.push(mp(a[i],0));
}else
{
int m=0;
while(st.size()&&st.top().fi<=a[i])
{
m=max(m,st.top().se);
st.pop();
}
if(st.empty())
m=-1;
st.push(mp(a[i],m+1));
ans=max(ans,m+1);
}
}
cout<<ans<<endl; return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

Psychos in a Line CodeForces - 319B (单调栈的应用)的更多相关文章

  1. CodeForces 548D 单调栈

    Mike and Feet Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Subm ...

  2. Discrete Centrifugal Jumps CodeForces - 1407D 单调栈+dp

    题意: 给你n个数hi,你刚开始在第1个数的位置,你需要跳到第n个数的位置. 1.对于i.j(i<j) 如果满足 max(hi+1,-,hj−1)<min(hi,hj) max(hi,hj ...

  3. Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

    B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/p ...

  4. Codeforces Round #189 (Div. 2) D. Psychos in a Line 单调队列dp

    D. Psychos in a Line time limit per test 1 second memory limit per test 256 megabytes input standard ...

  5. codeforces 319B Psychos in a Line(模拟)

    There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At eac ...

  6. Codeforces Round #305 (Div. 1) B. Mike and Feet 单调栈

    B. Mike and Feet Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/547/pro ...

  7. Codeforces 1107G Vasya and Maximum Profit 线段树最大子段和 + 单调栈

    Codeforces 1107G 线段树最大子段和 + 单调栈 G. Vasya and Maximum Profit Description: Vasya got really tired of t ...

  8. Imbalanced Array CodeForces - 817D (思维+单调栈)

    You are given an array a consisting of n elements. The imbalance value of some subsegment of this ar ...

  9. Codeforces 1107G Vasya and Maximum Profit [单调栈]

    洛谷 Codeforces 我竟然能在有生之年踩标算. 思路 首先考虑暴力:枚举左右端点直接计算. 考虑记录\(sum_x=\sum_{i=1}^x c_i\),设选\([l,r]\)时那个奇怪东西的 ...

随机推荐

  1. uni-app相关

    uni-app 中以下组件的高度是固定的,不可修改: 导航栏高度固定为 44pxtabBar 高度固定为 56px 状态栏比较特殊,是一个变量 .status_bar{ height: var(--s ...

  2. libvirt log系统分析

    1.编译和安装 配置参数需要加上–enable-debug=yes,相关定义在src/util/virlog.h文件中定义 图1-1 ENABLE_DEBUG宏 如果没有加这个编译参数,调用VIR_D ...

  3. 【Abode Air程序开发】在设备上进行安装

    在设备上进行安装 在 Google Android 设备上安装应用程序 在 Apple iOS 设备上安装应用程序 在 Google Android 设备上安装应用程序 在项目的开发.测试和部署阶段, ...

  4. C#枚举扩展方法,获取枚举值的描述值以及获取一个枚举类下面所有的元素

    /// <summary> /// 枚举扩展方法 /// </summary> public static class EnumExtension { private stat ...

  5. 前端web worker实践与总结

    参考链接:https://www.jianshu.com/p/97f6144dfddf

  6. SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)

    上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...

  7. 在Linux虚拟机里开启Apache服务

    首先第一步我们配置环境:把yum与Linux ping通 1.我们来下载apache服务,输入:yum install httpd * 2.安装完毕之后默认是死的,要给他启动一下,输入命令:syste ...

  8. Ubuntu18.04.3主力开发机使用记录(一)

    现在是2019年12月02日,在公司使用Ubuntu作为开发机器已经有一段时间了 查看主分区创建时间 安装时间8月26 当时周一,一个新的迭代刚刚开始,早上来到公司发现开不了机:Windows报错蓝屏 ...

  9. pyhanlp 繁简转换之拼音转换与字符正则化

    繁简转换 HanLP几乎实现了所有我们需要的繁简转换方式,并且已经封装到了HanLP中,使得我们可以轻松的使用,而分词器中已经默认支持多种繁简格式或者混合.这里我们不再做过多描述. ·说明 · Han ...

  10. PAT B1046.猜拳

    课本AC #include <cstdio> int main() { int n, failA = 0, failB = 0; scanf("%d", &n) ...