zoukankan      html  css  js  c++  java
  • 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/problem/319/B

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

    Sample Input

    10
    10 9 7 8 6 5 3 4 2 1

    Sample Output

    2

    HINT

    题意

    给你一堆数,每个数次都可以吃掉他右边连续减小的序列,问你多少轮之后,会变成上升序列

    题解:

    一开始想的暴力,用并查集/链表优化一下,结果不幸被t(其实也是情理之中……

    T的数据是 100000 1 2 3 4 5…… 99999

    正解是单调队列,维护一个单调下降的序列,就可以找到一直杀到哪儿的位置。

    f[i]表示杀人的次数

    代码

    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define test freopen("test.txt","r",stdin)
    #define maxn 1000005
    #define mod 10007
    #define eps 1e-5
    const int inf=0x3f3f3f3f;
    const ll infll = 0x3f3f3f3f3f3f3f3fLL;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    int a[maxn];
    int f[maxn];
    int t[maxn];
    int main()
    {
        int n=read(),top=0,ans=0;
        for(int i=0;i<n;i++)
            a[i]=read();
        for(int i=n-1;i>=0;i--)
        {
            int tt=0;
            while(top&&a[t[top-1]]<a[i])
                f[i]=tt=max(tt+1,f[t[--top]]);
            t[top++]=i;
        }
        printf("%d",*max_element(f,f+n));
    }
  • 相关阅读:
    【JavaP6大纲】MySQL篇:如何实现 MySQL 的读写分离?MySQL 主从复制原理是啥?如何解决 MySQL 主从同步的延时问题?
    【JavaP6大纲】MySQL篇:现在有一个未分库分表的系统,未来要分库分表,如何设计才可以让系统从未分库分表动态切换到分库分表上?
    快速搭建PHP开发环境(PhpStorm+EasyPHP)
    iBatis开发者手册翻译(目录)
    iBatis开发者手册翻译(章节一、引言)
    asp.net 数据操作三步曲(一) :)
    一事归一事
    闲言碎语话心得你给我多少钱
    研发过程之代码评审
    让她自己来
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4609626.html
Copyright © 2011-2022 走看看