码迷,mamicode.com
首页 > 其他好文 > 详细

Psychos in a Line

时间:2016-03-11 22:23:29      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:

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

 

 

 

Psychos in a Line

标签:

原文地址:http://www.cnblogs.com/seana/p/5267230.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!