zoukankan      html  css  js  c++  java
  • 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
    ",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
    */
     
  • 相关阅读:
    友盟页面统计
    为什么调用 FragmentPagerAdapter.notifyDataSetChanged() 并不能更新其 Fragment?
    让jQuery的ajaxFileUpload插件支持onchange事件
    sublime双击选择全选带中划线
    配置Chrome Workspace功能
    font-face 跨域解决
    Yahoo团队经验:网站性能优化的34条黄金法则
    require.js+knockout.js+.underscore模板引擎的使用
    使用livereload实现自动刷新
    WebStorm 7.0 支持更多的Web技术
  • 原文地址:https://www.cnblogs.com/jhz033/p/6628664.html
Copyright © 2011-2022 走看看