P - Psychos in a Line 单调队列

P - Psychos in a Line
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status
Appoint description: System Crawler  (2013-07-04)

Description

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

Sample Input

Input
10
10 9 7 8 6 5 3 4 2 1
Output
2
Input
6
1 2 3 4 5 6
Output
0

 

 
const int INF = 1000000000;
const double eps = 1e-8;
const int maxn = 301000;
typedef pair<int,int> ii;
int a[maxn];
int next[maxn];//链表指针
int main() 
{
    //freopen("in.txt","r",stdin);
    int n;
    while(cin>>n)
    {
        queue<ii> q;
        rep(i,0,n)
        {
            scanf("%d",&a[i]);
            next[i] = i+1;
        }
        
        repd(i,n-2,0)
        {
            if(a[i] > a[i+1])
                q.push(ii(i,0));
        }
        
        int ans = 0;
        while(!q.empty())
        {
            int pos = q.front().first;
            int cas = q.front().second;
            ans = max(ans,cas);
            q.pop();
            if(next[pos] != n && a[pos] > a[next[pos]])
            {
                q.push(ii(pos,cas+1));
                next[pos] = next[next[pos]];
            }
        }
        
        cout<<ans<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/DreamHighWithMe/p/3427926.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值