D. Psychos in a Line Codeforces Round #189 (Div. 2)

來源:互聯網
上載者:User

D. Psychos in a Linetime 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.

Sample test(s)input
1010 9 7 8 6 5 3 4 2 1
output
2
input
61 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.

思路:考慮第i個人是被誰殺的。他可以被他前一個人殺,如果殺不了,則被殺掉前一個人的人所殺,類推,找到能殺掉第i個人的人,若都殺不死他,則記錄不能殺死他。

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>using namespace std;int n,ans,flag;int a[100005],b[100005],xb[100005]; // a[]-存輸入的資料//  b[]-存需要幾次操作將他殺掉 xb[]-存殺掉第i個人的人的下標void solve(){    int i,j,ma;    ans=-1;    b[1]=xb[1]=0;     // 預設b[1]殺不掉    for(i=2; i<=n; i++)  //從第二個開始判斷    {        ma=0;        flag=0;     // 存a[i]是否能被殺掉        j=i-1;        while(a[j]<a[i]&&j)        {            if(!b[j])  // 如果a[j]不能被前面人殺掉 則a[i]也不能            {                flag=1;                break;            }            if(b[j]>ma) ma=b[j];            j=xb[j];   // 跳轉到殺死a[j]的人        }        if(!flag)   b[i]=ma+1;  //如果被殺死 則需ma+1步        else   b[i]=0;          // 沒被殺死則需要0步        xb[i]=j;       // 記錄是誰殺死他的//     printf("i:%d b[i]:%d xb[i]:%d\n",i,b[i],xb[i]);    }    for(i=1; i<=n; i++)    {        if(ans<b[i]) ans=b[i];    }}int main(){    int i,j;    while(~scanf("%d",&n))    {        for(i=1; i<=n; i++)        {            scanf("%d",&a[i]);        }        solve();        printf("%d\n",ans);    }    return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.