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 input
output
standard output

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
10
10 9 7 8 6 5 3 4 2 1
output
2
input
6
1 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.

 

   求无法杀人的时间;
思路:dp[i]表示i可以杀死右边的人需要的时间;
   从后往前
   需要维护一个单调递减的序列;
   如果你右边的人dp[i+1]需要t时间;
   然后你杀死它只需要一秒,你需要t时间替他杀完那多出来的人;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
#define bug(x)  cout<<"bug"<<x<<endl;
const int N=1e5+10,M=1e6+10,inf=2147483647;
const ll INF=1e18+10,mod=2147493647;
int a[N],dp[N];
int q[N];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        int head=0,tail=1;
        int ans=0;
        for(int i=n;i>=1;i--)
        {
            int sum=0;
            while(tail<=head&&a[i]>a[q[head]])
            {
                sum++;
                sum=max(sum,dp[q[head]]);
                dp[i]=max(sum,dp[q[head]]);
                head--;
            }
            ans=max(ans,dp[i]);
            q[++head]=i;
        }
        printf("%d\n",ans);
    }
    return 0;
}
/*
10
10 7 4 2 5 8 9 6 3 1
4  2 1 0 0 0 1 1 1 0
4
*/

 

 

转载于:https://www.cnblogs.com/jhz033/p/6628664.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值