codeforces 319B Psychos in a Line(模拟)

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 ≤ 10^5). 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.

 

题目大意:每一ROUND如果某个人的数字大于他右边的人,他就会干掉右边的人,一个人可以同时干掉别人和被干掉,问要多少ROUND结束后才不会死人。

思路:大水题,CF你标什么数据结构……初始化每个人给一个杀人的状态(如果他大于右边的人),如果他不能杀人了就取消他的状态(没有重新获得杀人状态的可能性,这里不证),方便删除用链表记录这些有杀人状态的人,一ROUND一ROUND地模拟即可。

PS:顺便研究了一下list怎么用(迭代器居然不能直接用加号和减号可恶),忘了删调试代码又WA了一次(刚好最后测的那个数据没有把调试代码的东西打出来结果忘了就交上去了>_<)

 

代码(63MS):

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <iostream>

 4 #include <algorithm>

 5 #include <list>

 6 using namespace std;

 7 

 8 const int MAXN = 100010;

 9 

10 list<int> killing;

11 list<int>::iterator it, it2;

12 

13 int a[MAXN], next[MAXN];

14 int n;

15 

16 int main() {

17     scanf("%d", &n);

18     for(int i = 1; i <= n; ++i) {

19         scanf("%d", &a[i]);

20         next[i] = i + 1;

21         if(a[i - 1] > a[i]) killing.push_back(i - 1);

22     }

23     a[n + 1] = n + 1;

24     int step = 0;

25     while(!killing.empty()) {

26         ++step;

27         it = it2 = killing.end();

28         --it2;

29         bool flag = true;

30         //for(list<int>::iterator p = killing.begin(); p != killing.end(); ++p) printf("%d\n", a[*p]);

31         do {

32             it = it2--;

33             //printf("%d %d %d\n", *it, *it2, a[*it]);

34             int now = *it, now2 = *it2;

35             next[now] = next[next[now]];

36             if(it == killing.begin()) flag = false;

37             if(a[now] < a[next[now]] || (it != killing.begin() && next[now2] == now)) killing.erase(it);

38         } while(flag);

39     }

40     printf("%d\n", step);

41 }
View Code

 

你可能感兴趣的:(codeforces)