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';
		}
	}
}