Psychos in a Line

本文探讨了神经病排列优化问题,通过特定的杀戮规则来减少死亡次数直至稳定状态。利用单调栈算法解决,涉及模拟、数据结构与算法等知识点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Psychos in a Line

TimeLimit:1000MS  MemoryLimit:256MB
64-bit integer IO format:%I64d
Problem 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.

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

 

题意:

  一堆神经病站成一列, 神经病拥有的 id 如果 大于 右边的神经病的 id, 就可以砍死对方。

需要注意的是, 5 4 3 变成 5, 只需一步, 即是说符合条件的话, 可以同时砍右边的神经病。

 

一开始连题意都不懂, 真是糟糕。

后来经过学长的题解, 题意才渐渐明白。可是题解的理解, 还是有点懵逼。真是惭愧。

所以, 就放弃治疗, 自己去探索。

举例子吧。

给 15 个神经病人。id 如下

        15  9  5  10  7  11  14  6  2  3  12  1  8  13  4

干掉变化如下:

  第一步:  15 10 11  14  3  12 8 13

  第二步:  15 11 14  12 13

  第三步:    15 14 13

  第四步:  15

 

到这里, 其实也是一脸懵逼。

就想在原来的数列上去表上各个 id 是在第几步被干掉的。 然后再去模拟 id 被干掉的步数即可。其实我觉得也算是模拟题吧。

干掉步数:  + 1  1   2   1   3    4   1  1  2   3  1  2    4  1

原id:    15  9  5  10  7  11  14  6  2  3  12  1  8  13  4

用 单调栈去做。若是比之前的 id 大, 则 在之前的 id 的 次数 上 + 1 与之 取 最大。 需要注意的是, 当 栈 为 0 ,该 id  次数需要初始化为 0, 若是比栈中的 id 小, 若是第一次比 前一个数小, 则 次数 初始化为 1, 否则 之前 复制栈中 id 的次数。

code:

#include <cstdio>
using namespace std;
const int all = (1e5)+5;
int maxnum[ all ], con[ all ], stac[ all ], t, cnt;
bool isfirst;
#define Max( i, j )  (i) > (j) ? (i) : (j);
int main(void)
{
    scanf( "%d", &t );
    for( int i=0; i < t; ++ i ){
        scanf( "%d", con+i );
    }

    isfirst = true;
    for( int i=0, j=0; i < t; ++ i, ++ j ){
        if( j && con[i] > stac[j-1] ){
            while( j && con[i] >stac[j-1] ){
                -- j;
                maxnum[ con[i] ] = Max( maxnum[ con[i] ], maxnum[ stac[j] ] + 1 );
                stac[ j ] = con[i];
            }
            isfirst = true;
        }
        else{
            if( isfirst ){
                maxnum[ con[i] ] = 1;
            }
            else{
                maxnum[ con[i] ] = maxnum[ stac[j-1] ];
            }
            stac[j] = con[i];
        }

        if( !j ){
            maxnum[ stac[j] ] = 0;
        }
    }

    cnt = 0;
    for( int i=0; i < all; ++ i ){
        cnt = cnt > maxnum[i] ? cnt : maxnum[i];
    }
    printf( "%d\n", cnt );

    return 0;
}
View Code

 

 

 

转载于:https://www.cnblogs.com/seana/p/5267230.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值