We need to understand the problem: "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."
We have an initial permutation. At each step, simultaneously, for each position i (1-indexed from left) if there is a right neighbor at i+1, and the id at i > id at i+1, then the psycho at i kills the one at i+1. After all killings occur simultaneously, the killed psychos are removed, and the line compacts (so the survivors are in original order). Then next step occurs. The process continues until there is no killing in a step (i.e., the line becomes increasing from left to right, because if it's increasing, no left element > right element). Actually note: the condition for a kill is left > right, so it's descending adjacent pairs that cause killing. If the line is non-decreasing? Actually strictly increasing ensures no kill. But what about equal? IDs are unique so no equal. So final stable configuration must be strictly increasing from left to right. Also possibly there could be multiple psychos left? Yes, the process stops when the permutation is increasing. In sample: [10,9,7,8,6,5,3,4,2,1] -> after first step: they said [10,8,4]. Let's verify: At step 1, simultaneously each left > right kills neighbor. Check pairs:
10 > 9 kill: 9 dies (by 10)
9 > 7? But 9 might be killed? It doesn't matter: at same step, the killing decisions are based on original line before removals. So 9 sees its right neighbor 7; 9 > 7, so 9 would kill 7. But if 9 itself is killed by 10, does it still kill? The statement: "Note that a psycho might kill and get killed at the same step." So indeed, even if a psycho is killed, it can also kill its neighbor. So 9 kills 7, even though 9 dies. Similarly, 7 > 8? 7 < 8, so no kill. 8 > 6? Actually 8 > 6 so 8 kills 6. But 8 might also be killed? Its left neighbor 7 does not kill it because 7<8, so 8 survives? But 8 also kills 6. Then 6 > 5, kills 5; 5 > 3, kills 3; 3 > 4? 3 <4, no kill; 4 > 2, kills 2; 2 > 1, kills 1. So after simultaneous killings, the killed ones are: 9,7,6,5,3,2,1? Wait: Let's list victims:
- 9 killed by 10 (since 10 > 9)
- 7 killed by 9 (since 9 > 7)
- 6 killed by 8 (8 > 6)
- 5 killed by 6 (6 > 5)
- 3 killed by 5? Actually 5 > 3, so 5 kills 3? But 5 is being killed by 6, but still can kill. So 3 is killed.
- 4? check: 4 is killed? left neighbor 3 does not kill because 3<4; right neighbor? 4's right neighbor 2, and 4 > 2, so 4 kills 2; but is 4 killed? left neighbor 3 didn't kill, so 4 survives if not killed by someone else. However, there might also be a kill from left? Actually only left neighbor can kill. So 4 not killed by left (3). So 4 survives? Wait, but also note: 4's left neighbor 3 is killed and maybe 3 could kill 4? No, condition is left > right, so 3 cannot kill 4. So 4 is safe from left. So 4 should survive.
- 2 killed by 4? Actually 4 > 2, so 4 kills 2. So 2 is victim.
- 1 killed by 2? 2 > 1, so 2 kills 1, but 2 is killed by 4, but still kills. So 1 is victim.
Also consider 8: left neighbor 7 did not kill 8, so 8 survives. 10: left neighbor none, survives.
So survivors: 10, 8, 4? Let's list original indices:
1:10 (survives)
2:9 (killed)
3:7 (killed)
4:8 (survives)
5:6 (killed)
6:5 (killed)
7:3 (killed)
8:4 (survives)
9:2 (killed)
10:1 (killed)
So survivors: 10,8,4. That's three numbers. Indeed they gave [10,8,4]. Next step: [10,8,4]. Check pairs: 10 > 8, so 10 kills 8; 8 > 4, so 8 kills 4. Both 8 and 4 are killed? But note: 8 might be killed by 10, but also kills 4. So survivors: only 10. So step 2 yields [10]. Step 3: only one element, no kills, so stop. So steps count = number of steps until no kills: after step 2, line is stable (since no neighbor). So answer 2.
Thus process is: repeatedly, in parallel, any adjacent pair with decreasing order (left > right) results in the right neighbor being killed. However, the left neighbor may also be killed by its own left neighbor. But the kills happen simultaneously based on initial configuration of that step.
We need to compute number of steps until the sequence is increasing.
n up to 1e5, so we need O(n log n) or O(n) solution.
This problem is known: Codeforces problem 319B (Psychos in a Line). There is known solution using stacks or DP. I recall there is a classic problem: "Psychos in a Line" where we need to compute the maximum number of rounds a particular psycho can survive, or total rounds? Actually there is a problem "319B - Psychos in a Line" from Codeforces. The problem asks: given the initial arrangement, find the maximum number of seconds that a psycho will live. Or wait, I recall two versions: One where they ask the maximum number of steps (time) before all killings stop? Actually I recall Codeforces Round #189 (Div. 1) problem A: "Psychos in a Line". Let me recall: The problem: There are n psychos standing in a line. Each psycho has a unique ID from 1 to n. In each second, each psycho who has a larger ID than his right neighbor kills his right neighbor. Those killed are removed instantly at the same time. The process continues until no psycho kills. The question: what is the maximum time a psycho lives? Or number of steps until process stops? I'm not entirely sure. Let me check memory: Actually I recall a problem "Psychos in a Line" from Codeforces: In that problem, they ask for the maximum number of seconds a psycho will live (i.e., the maximum number of steps until he gets killed). However, this problem statement says: "Calculate how many steps are needed to the moment of time such, that nobody kills his neighbor after that moment." That's the total number of rounds until stability. So it's the total steps needed. However, the sample matches the CF problem output? In CF 319B sample: Input: 10; 10 9 7 8 6 5 3 4 2 1; Output: 2. That matches. But I think CF problem actually asks for the maximum number of steps any psycho lives (i.e., the time when the last killing happens). In the sample, the last killings happen at step 2? Actually the last killings happen at step 2, so maximum time a psycho lives is 2? But some psychos die at step 1, some at step 2. The maximum is 2 (psycho 8 and 4 die at step 2). So the answer would be 2. So both interpretations would give 2? Possibly it's the same. Let's check second sample: 1 2 3 4 5 6; no kills, so max time any psycho lives is 0, steps to stability is 0. So both are 0. So they could be same for these samples. But I'm not 100% if they always coincide. Let's think: The total steps until stability is essentially the number of rounds needed until no kills; that equals the maximum over all psychos of the round in which they are killed (or 0 if they survive). Because the process ends when no more kills happen, which is exactly after the last round where any kill happens. That last round is the maximum death time among those who die. Survivors have no death time, but they don't affect the end time. So total steps = max_{i killed} (time of death of i). In sample, max death time is 2, so steps = 2. So it's equivalent. However, if there is a psycho that never dies, that would imply no more kills after some round? Actually if there is a survivor, the process could end earlier because there are no more kills. So the total steps is indeed the maximum death time (or 0 if none). So problem reduces to finding the maximum "time" (step) when a psycho is killed.
Thus it's exactly Codeforces 319B. So we can adopt solution from that.
Now we need to compute, for each element, the step at which it gets killed. The process: at each step, any element that has a larger left neighbor (immediate left) that is alive at the start of that step? Actually kills happen based on current ordering at start of step. Since kills are simultaneous, an element may be killed if its left neighbor's id > its id, regardless of whether the left neighbor gets killed in the same step. So the survival of an element from one step to next depends on whether it's killed by its left neighbor at that step.
We can simulate the process efficiently by noting that it's similar to a stack-based elimination where elements "eat" smaller elements to their right gradually. The process can be modeled by something like: consider the permutation, we want to compute for each position its "death time" if any. We can think of the kills as occurring in waves: At each step, any element that has a larger immediate left neighbor will die. However, after removal, the left neighbor might then face a new right neighbor, which might be larger or smaller. This is reminiscent of the problem of eliminating decreasing runs, like "Alien's trick"? Actually it's akin to repeatedly removing all elements that have a larger left neighbor.
We can think of it as: In each round, we remove all "valleys" or "peaks"? Not exactly. For each index i, if a[i] > a[i+1], then a[i+1] will be killed in that round (provided that round is being considered). So it's like a[i+1] gets eliminated if it's to the right of a larger element. So we need to find after how many rounds each element gets eliminated.
I recall that in CF 319B, the solution uses a stack and computes the number of rounds each element "survives" as the maximum of something along a chain of decreasing values? Or maybe it's DP: For each i, let dp[i] be the number of rounds needed to eliminate that element, given that it's killed by its left neighbor at some time, and the left neighbor might be killed later. There is a known solution: Process the array from left to right, maintain a stack of pairs (value, time). For each new element, initialize its death time to 0. Then while the top of stack has value greater than current element, we pop and update current element's death time to max(current death time, popped death time) + 1? Something like that. I need to reconstruct.
I've solved this problem before: The algorithm: We iterate from left to right. Maintain a stack of the survivors (or maybe of the elements along with the number of rounds they have "survived" or the time they kill). Actually the typical solution: We want to compute for each element the number of rounds it will live (i.e., when it will be killed). Let death[i] = step number when a[i] gets killed (0 if never killed). We'll process indices from left to right, maintaining a stack of indices (or values and death times) that are "alive" in the current state after considering some elements? Actually we simulate the process after all rounds? Let's derive.
Consider the process: It's similar to a "cascade" of eliminations. Observations:
-
An element will be killed only by its immediate left neighbor (in the current line at the moment of killing). Over multiple rounds, after some eliminations, an element may get a new left neighbor (the previous left neighbor may have been eliminated, and now the left neighbor is the first alive element to the left). That new left neighbor could be larger, causing the element to die in a later round.
-
The death time of an element can be thought of as the length of a chain of decreasing values to its left that get eliminated in certain order. Another viewpoint: The process is similar to repeatedly eliminating elements that are "dominated" by the next alive left neighbor.
One known solution: simulate rounds efficiently by grouping elements into "levels". At each round, we remove all elements that have a larger neighbor to the left at the beginning of the round. So we can keep track of for each element the round it gets removed. This is like: if we consider the array, we can process it from left to right, maintaining a stack of the elements that are currently "alive" after processing up to current position, but with their removal times computed. However, the process is not simply left-to-right because later elements may be killed by earlier ones after some rounds, not immediately.
Actually think: When we process the array from left to right, we can think of building the final increasing sequence? The final survivors are those that are in increasing order from left to right? Wait, the process ends when the line is increasing. So the final survivors are exactly the "maximal increasing subsequence"? Not necessarily: In sample, final survivor is 10 only, which is the maximum element. Actually let's check: If we have permutation, after process, all decreasing pairs are eliminated. The process will eventually result in a strictly increasing sequence. But what is that increasing sequence? It seems to be the set of elements that are "record highs" when scanning from left? For [10,9,7,8,6,5,3,4,2,1], scanning left to right, the record highs (greater than all previous) are: 10 (first), then 9 <10 no, 7<10, 8<10, 6<10,5,3,4,2,1. So only 10 is a record high. Final result is [10]. So final survivors might be the set of elements that are left-to-right maxima? In second sample all are record highs, final survivors = all. In general, if we have a permutation, is it always that the survivors are exactly the left-to-right maxima? Let's test with small examples.
Consider [2,1,3]. Process:
Step1: 2 >1, so 2 kills 1. Simultaneously 1's right neighbor? 1 has right neighbor 3; 1<3 so no kill. So after step1: survivors: 2,3. Sequence [2,3] is increasing, so stop. Final survivors: 2,3. Left-to-right maxima: scanning: 2 is record, then 1 <2 not, 3 >2 is record. So records: 2,3. Yes.
Consider [3,1,2]. Step1: 3>1 so 1 dies; 1<2 no kill. So survivors: 3,2. Now sequence [3,2]: 3>2 so 2 dies in step2? At step2, 3>2 so kill. After step2: [3]. Final: 3. Left-to-right maxima: 3, then 1<3 not, 2<3 not, so only 3. Yes.
Consider [1,3,2]. Step1: 1<3 no kill; 3>2 kills 2. Survivors: [1,3]. This is increasing? 1<3, yes, so stop. Final survivors: 1,3. Left-to-right maxima: 1 (record), 3 (record), 2 not. So yes.
Consider [2,3,1]. Step1: 2<3 no kill; 3>1 kills 1. Survivors: [2,3]. increasing -> stop. Left-to-right maxima: 2,3,1 not. Yes.
Consider [3,2,1,4]. Step1: 3>2 kills2, 2>1 kills1, 1<4 no kill. Simultaneous: victims:2,1. Survivors:3,4? Also check: 2 is killed but also kills1, so survivors: 3 (since 3 not killed), 4 (since left neighbor 1 was killed, but 4 remains). Wait careful: After step1, which elements remain? positions: 1:3, 2:2, 3:1, 4:4. Kills: 3 kills 2; 2 kills 1; 1 does not kill 4 because 1<4. So victims:2 and 1. So survivors:3 and 4. Sequence: [3,4] increasing -> stop. Left-to-right maxima: 3 (record), then 2<3 no, 1<3 no, 4>3 record -> 3,4. Yes.
Consider [4,1,2,3]. Step1: 4>1 kills1; 1<2 no kill; 2<3 no kill. Survivors: 4,2,3? Actually victims:1. So line becomes [4,2,3]. Step2: 4>2 kills2; 2<3 no kill. Survivors:4,3. Step3: 4>3 kills3. Survivors:4. So steps=3. Left-to-right maxima: 4, then 1<4,2<4,3<4 => only 4. Final survivor:4. So matches.
It appears final survivors are exactly the left-to-right maxima (records). Is that always true? Let's think: In the process, any element that is not a left-to-right maximum will eventually be killed? Because if an element x is not a left-to-right maximum, then there exists some element to its left that is larger than x. However, that larger element might get killed before it kills x? But the process might eliminate that larger element before it gets to kill x, maybe x survives? For example, consider [5,1,4,2,3]. Left-to-right maxima: 5, then 1<5, 4<5? Actually 4<5 so not record, 2,3<5. So only record is 5. But will the final survivor be only 5? Let's simulate: Step1: 5>1 kills1; 1<4 no kill; 4>2 kills2; 2<3 no kill. Victims:1,2. Survivors:5,4,3 => [5,4,3]. Step2: 5>4 kills4; 4>3 kills3. Victims:4,3. Survivors:5. Step3: only 5. So final:5. Yes.
Consider [2,4,1,3]. Left-to-right maxima: 2,4 (since 2 record, 4>2 record). 1 and 3 not. Simulation: Step1: 2<4 no kill; 4>1 kills1; 1<3 no kill. Survivors:2,4,3 => [2,4,3]. Step2: 2<4 no; 4>3 kills3. Survivors:2,4 => [2,4] increasing, stop. Final survivors:2,4 which are exactly left-to-right maxima. So indeed survivors are left-to-right maxima.
But is it always? Suppose we have [3,5,2,4]. Left-to-right maxima: 3,5, then 2<5,4<5 so only 3 and 5 are records. Simulate: Step1: 3<5 no; 5>2 kills2; 2<4 no. Survivors:3,5,4 => [3,5,4]. Step2: 3<5 no; 5>4 kills4. Survivors:3,5 => increasing. So final:3,5. Yes.
What about [4,2,5,1,3]? Records:4,5. Simulate: Step1:4>2 kills2;2<5 no;5>1 kills1;1<3 no. Survivors:4,5,3 => [4,5,3]. Step2:4<5 no;5>3 kills3. Survivors:4,5 => increasing. So final:4,5.
What about [3,2,5,4,1]? Records:3,5. Sim: Step1:3>2 kills2;2<5 no;5>4 kills4;4>1 kills1. So victims:2,4,1. Survivors:3,5 => [3,5] increasing. So final:3,5.
It seems plausible that final survivors are exactly the left-to-right maxima. Let's try to prove: At any step, the leftmost element always survives (since no left neighbor to kill it). Actually leftmost could be killed? No, because only left neighbor can kill; leftmost has no left neighbor, so it never gets killed. So the first element always survives. But is the first element always a left-to-right maximum? It is by definition a record because it's the first. So okay. Next, consider the second element: if it is greater than the first, it's a record and might survive? But can it be killed? It could be killed if its left neighbor (first) is greater. Since first is smaller, it's not killed. However, it could be killed later by some element that becomes its left neighbor after removal of first? But first never dies, so left neighbor will always be first if it's alive. But if first is smaller, then no kill. So any element that is a record (greater than all previous) will never be killed because all elements to its left are smaller, so none can kill it (since kill condition requires left > right). However, it could be killed by a left neighbor that appears after some removals? But the left neighbor of a record is the immediate previous survivor; if that previous survivor is smaller, no kill. If later, some larger element might become its left neighbor if something in between gets eliminated and a larger element from further left moves next to it. But note: left-to-right maxima are increasing; they are the peaks. The left neighbor of a record will always be the previous record (because all non-record elements in between will eventually be eliminated, leaving only records). Since records are in increasing order (each record is larger than all previous), the previous record is smaller, so cannot kill. So indeed records survive.
Conversely, if an element is not a record, there exists some larger element to its left. That larger element may be eliminated later, but before it is eliminated, it might kill this element. However, if that larger element gets eliminated before it can kill? But kills happen simultaneously. At step 1, all pairs where left > right cause the right to be killed. So if a non-record element x has a larger element y immediately to its left at step 1? Not necessarily: there could be some elements between y and x. But eventually after some eliminations, y might become its immediate left neighbor. But will y kill x at that time? Possibly. But could x survive if all larger elements to its left are eliminated before they become its immediate neighbor? That seems unlikely because for y to be eliminated, it must be killed by some even larger element to its left. That even larger element is also larger than x. So the chain: we have ... z > y > ... > x with z > y. If y gets killed by z at some step, then after y dies, z becomes the left neighbor of x (if there is no other survivors between them). Then z > x, so z will kill x in the next step (unless z also gets killed in that same step? But if z kills y, then after y's removal, z's right neighbor becomes x. In the next step, if z > x, then z kills x. However, if z also gets killed by its own left neighbor at the same step as it kills y? Possibly, but if z gets killed, then x might survive? But note: kills are simultaneous. Let's think carefully: Suppose we have ... w, z, y, x ... with w > z, z > y, y > x? Actually we need to model the process: At each step, based on current line, any left > right kills right. So in a step, both w > z and z > y and y > x could all happen simultaneously. So if w > z, then z is killed. At the same time, if z > y, then y is killed (even though z is being killed). And if y > x, then x is killed. So in a single step, multiple kills can cascade. So the fact that a larger element gets killed does not necessarily save the smaller to its right if that smaller also gets killed by some other element in the same step.
But could a non-record element survive indefinitely? For it to survive, it must never be killed. That means at every step, its immediate left neighbor (if exists) is smaller. But initially, there is some larger element to its left somewhere. Over time, elements between it and that larger element may be removed, eventually making that larger element its immediate left neighbor. At that moment, if the larger element is still alive and smaller than it? No, larger element is > it. So at the start of that step, the larger element is left neighbor and > it, so it will be killed. Unless the larger element gets killed at the same step, but even if it does, it still kills its right neighbor. So the killing action occurs regardless of whether the left neighbor dies. So that step, the non-record element would be killed by the larger neighbor. The only way it could avoid being killed is if the larger neighbor never becomes its immediate left neighbor, i.e., if some other element always remains between them. But all elements between it and the larger neighbor are smaller than the larger neighbor (maybe some larger than it?). Actually the larger neighbor is the first larger to the left. But there may be other elements in between that are also smaller than that larger neighbor. They could potentially survive? But are they records? Possibly not. Over time, those in-between elements will be eliminated eventually, because they are not records (since they have a larger element to their left). So they will eventually die, leaving the larger element adjacent to x. So indeed x will eventually be killed. However, there is nuance: The larger element may itself be killed before the in-between elements are all removed. If the larger element dies, then the next candidate to the left could be even larger, which also would kill x when adjacent. So eventually some larger element will become its left neighbor and kill it. The only way x could survive forever is if all larger elements to its left die before they become its immediate left neighbor, and the eventual left neighbor is smaller. But is it possible that all larger elements die while there's still a smaller element between? Let's imagine a scenario: consider [2,3,1]. Here 1 is not a record (since 2,3 > 1). Larger elements: 2 and 3. However, 3 is the first larger from left? Actually from left: 2 <3, so 3 is record, 2 is record as well? 2 is first so record. 3 is larger than 2. So both records. In simulation, 1 dies at step1 by 3? Actually step1: 2<3 no; 3>1 kills1. So 1 dies. So no survival.
Try to construct a non-record that survives: Suppose we have [4,2,5,3]. Here 3 is not a record because left has 5>3. Records:4,5. Sim: Step1:4>2 kills2;2<5 no;5>3 kills3. So 3 dies. So no.
What about [5,1,3,2,4]? Records:5, maybe 4? 4 >5? No, 4<5, so not record. Actually scanning: 5, then 1<5, 3<5, 2<5, 4<5 so only 5 is record. But final survivors? Sim: Step1:5>1 kills1;1<3 no;3>2 kills2;2<4 no. Survivors:5,3,4 => [5,3,4]. Step2:5>3 kills3;3<4? Actually after step2, at start of step2 line is [5,3,4]. Pairs: 5>3 kills3; 3<4 no kill. So victims:3. Survivors:5,4 => [5,4]. Step3:5>4 kills4. Survivors:5. So final:5. So only record survives.
It appears final survivors are exactly left-to-right maxima. This can be proven: After process ends, the line is increasing, so survivors are increasing. Since survivors are increasing and include the first element, they must be a subsequence of the original permutation. Moreover, any element that is not a left-to-right maximum cannot be among the survivors because there exists a larger element to its left, and that larger element, if it survives, would be to its left in the final line (since survivors preserve original order). Then if it survives, we would have a decreasing pair (larger left, smaller right) contradiction. But could that larger element not survive? If it doesn't survive, then some even larger element to its left must survive, and that would be to its left, again causing a decreasing pair. So ultimately, the survivors must be a set such that each survivor is larger than all survivors to its left? Actually they must be increasing, so each survivor is greater than the previous survivor. Since survivors are in original order, this implies each survivor is larger than all previous survivors, which means each survivor is a left-to-right maximum with respect to the original sequence? Not exactly: If we pick a subsequence that is increasing, it might not include all left-to-right maxima; e.g., original: [2,5,3,4]. Left-to-right maxima: 2,5. An increasing subsequence could be [2,3,4] (if 3 and 4 survive?). But is that possible? Let's test simulation: [2,5,3,4]. Step1:2<5 no;5>3 kills3;3>4? Actually 3<4 no. So victims:3. Survivors:2,5,4 => [2,5,4]. Step2:2<5 no;5>4 kills4. Survivors:2,5 => increasing. So final survivors:2,5. So [2,3,4] not possible. What about [5,2,4,3]? L-to-R max:5, then 2<5, 4<5, 3<5 so only 5. But could 2,4,3 survive? Simulation: Step1:5>2 kills2;2<4 no;4>3 kills3. Survivors:5,4 => [5,4]; step2:5>4 kills4; final 5. So no.
What about [1,3,2,4]? L-to-R max:1,3,4. Final survivors:1,3,4 as we saw? Actually simulation gave final:1,3? Wait earlier we had [1,3,2,4]: Step1:1<3 no;3>2 kills2;2<4 no. Survivors:1,3,4 => [1,3,4]; Step2:1<3 no;3<4 no. So final:1,3,4. So survivors are exactly 1,3,4 which are left-to-right maxima. Indeed.
What about [1,2,4,3]? L-to-R max:1,2,4. Sim: Step1:1<2 no;2<4 no;4>3 kills3. Survivors:1,2,4 => increasing, final 1,2,4. So matches.
What about [3,1,2,5,4]? L-to-R max:3,5. Sim: Step1:3>1 kills1;1<2 no;2<5 no;5>4 kills4. Survivors:3,2,5 => [3,2,5]. Step2:3>2 kills2;2<5 no. Survivors:3,5. Final:3,5. So matches.
It seems survivors are indeed the left-to-right maxima. I'll try to find counterexample: Let's brute mental: n=5, permutation: [2,4,1,5,3]. L-to-R max:2,4,5. Sim: Step1:2<4 no;4>1 kills1;1<5 no;5>3 kills3. Survivors:2,4,5 => [2,4,5] increasing -> final 2,4,5. So matches.
[4,2,3,1,5]. L-to-R max:4,5? Actually 4, then 2<4, 3<4, 1<4, 5>4 so 5. So records:4,5. Sim: Step1:4>2 kills2;2<3 no;3>1 kills1;1<5 no. Survivors:4,3,5 => [4,3,5]. Step2:4>3 kills3;3<5 no. Survivors:4,5 => increasing, final 4,5. Matches.
[5,2,4,3,1]. L-to-R max:5. Sim: Step1:5>2 kills2;2<4 no;4>3 kills3;3>1 kills1. Survivors:5,4,1? Wait victims:2,3,1? Actually let's list: positions: 5,2,4,3,1. Kills: 5>2 kills2; 2<4 no; 4>3 kills3; 3>1 kills1. So survivors:5,4? also check 3 killed? yes; 1 killed? yes. So survivors:5 and 4. So [5,4]. Step2:5>4 kills4. Final:5. So only record.
Thus survivors = left-to-right maxima. So we can safely assume that the process ends when only left-to-right maxima remain.
But the problem asks for number of steps until stability. That's equal to the maximum number of rounds any non-record element survives (or the maximum depth of elimination). We need to compute that efficiently.
Now the problem is known: compute the maximum "time" a psycho will live. There are typical solutions:
- Simulate the process in rounds using a list/vector of survivors, iterating until no kills. But worst-case O(n^2) if many rounds (could be up to n?). Actually the number of rounds could be O(n) (e.g., descending order: n, n-1, ..., 1 yields n-1 steps? Let's test: n=5 descending: [5,4,3,2,1] -> step1: kills 4,3,2,1? Actually kills: 5 kills 4; 4 kills 3; 3 kills 2; 2 kills 1. So survivors:5. So steps=1. Wait descending yields just 1 step? Actually in descending, all get killed in first step because each left > right. So only 1 step. But consider pattern: [1,2,3,4,5] yields 0. What about maximum steps? I recall from CF that answer can be up to n-1 maybe. Let's think: The sample had 2 steps for 10 elements. There might be sequences that require many steps, like [n,1,2,3,...,n-1]? Let's simulate n=5: [5,1,2,3,4]. Step1:5>1 kills1;1<2 no;2<3 no;3<4 no -> survivors:5,2,3,4 => [5,2,3,4]. Step2:5>2 kills2;2<3 no;3<4 no -> survivors:5,3,4 => [5,3,4]. Step3:5>3 kills3;3<4 no -> survivors:5,4 => [5,4]. Step4:5>4 kills4 -> survivors:5. So steps=4. So steps = n-1? For n=5, steps=4 = n-1. So maximum steps is n-1. So O(n) steps is possible, but simulating each round by scanning the entire line could be O(n^2) because each round might require scanning O(survivors) and there could be O(n) rounds, giving O(n^2) worst-case. With n=1e5, too slow.
Hence need a smarter method.
Observing the process: At each round, we remove elements that have a larger left neighbor at the beginning of the round. This is similar to an elimination process often solved by stack/DP that yields O(n) or O(n log n).
Idea: For each element, compute the round in which it is killed. The answer is max of those rounds (or 0). This can be done by processing left to right and maintaining a stack of elements that are still "alive" in the context of computing death times. The algorithm from known editorial:
Let dp[i] be the number of seconds after which the i-th psycho (in the original order) dies (0 if never dies). We process i from 0 to n-1. Maintain a stack (or list) of indices of psychos that are currently alive (their death times not yet determined? Actually they are alive in the simulation of processing). For each new psycho at index i, set its death time to 0 (initially). Then while the stack is not empty and the value at the top of stack is less than a[i]? Wait: The condition for a[i] to be killed is if there exists a larger element to its left that becomes its neighbor. But in terms of stack, we might be representing the survivors after processing up to i, with their death times known. When we encounter a new element, we need to determine its death time based on those to its left that could kill it. Since kills happen from left to right? Let's think.
Consider the process: At each step, an element can be killed by its immediate left neighbor. So to determine when element i dies, we need to know the "chain" of larger elements to its left that might become its neighbor after some eliminations. Actually there is a known recurrence: If we consider the stack of elements that are "candidates" to kill future ones. When we have a new element x, we look at elements on the stack (to its left) that are greater than x. Among those, the ones that are closer may kill x earlier, but due to simultaneous eliminations, the death time of x is determined by the maximum death time of elements that are popped? Let's recall the solution from memory:
We maintain a stack of pairs (value, time). For each new element v:
- Initialize its death time t = 0.
- While stack not empty and stack.top().value < v: pop? Hmm, typical solution from problem "319B" uses stack where we maintain monotonic decreasing? Actually I've seen an editorial: "For each i from 0 to n-1, we maintain a stack of the indices of the psychos that are still alive (i.e., that haven't been killed yet) and also compute the number of seconds they survive. When we process a new psycho, we set its death time to 0. Then we pop from the stack all psychos that have a smaller id than the current one, and update the current psycho's death time as max(current death time, popped death time) + 1? Not exactly.
Let me search my memory: There is a known solution for "Psychos in a Line" using a stack with a monotonic decreasing sequence and counting the number of rounds an element survives. Actually I recall a solution where we simulate the process by storing for each element the "level" of elimination. The level of an element is the number of times it survives before being killed. And the answer is the maximum level.
We can think of the process as: In each round, any element that has a larger left neighbor is eliminated. This is analogous to repeatedly removing elements that are "dominated" by the previous element in terms of being smaller. This process can be seen as repeatedly "eating" the next smaller element. If we view the array from left to right, we can think of constructing a tree where each element kills the next smaller element(s) that are to its right until it encounters a larger element. Actually there is a concept of "kill tree" where each element kills the next element that is smaller than it and then maybe the next after that if that next is smaller than it after previous kills? But it's simultaneous.
Alternative approach: We can simulate using a linked list and process rounds by scanning and marking which ones get killed based on their left neighbor being larger. Since each element is killed exactly once, the total number of kills is n - (#survivors). But we need to know the round in which each gets killed. We could simulate rounds but in a smarter way: For each element, we can compute the round it gets killed based on when its "killer" gets killed? Possibly we can think of DP: Let dp[i] = number of steps after which the i-th psycho is killed (0 if never killed). For each i, we need to find the nearest left element that is greater than a[i] and that survives long enough to kill i? But note that an element could be killed by an element that is not its immediate left neighbor initially, but after some eliminations, the killer becomes adjacent.
Observation: The process is similar to repeatedly removing all elements that are "less than" the previous element. In other words, we can think of it as an iterative "stack" reduction: At each step, we remove any element that is less than the element to its left. This is analogous to repeatedly applying the following: while there exists i such that a[i] > a[i+1], remove a[i+1] (simultaneously). So we want to know how many iterations are required to reach an increasing sequence.
We can view this as: For each element, we want to know the length of a chain of "domination" where each element kills the next if it's smaller, but then after being killed, the killer might then kill the next if it's smaller, etc., but only within same round? Actually within a single round, an element can kill at most one neighbor (the immediate right). So the cascade happens across rounds.
Another perspective: Consider building the final increasing sequence (the records). For each element that is not a record, it will be killed at some round. That round can be computed as the maximum of something along a chain: Suppose we have records r1 < r2 < r3 ... (in terms of values, but note records are increasing in value and also in position). Actually records are increasing in value, but they might not be in increasing order? They are increasing by definition: each record is larger than all previous, so yes they are increasing in value. The non-record elements are those that are not records. For a given non-record element x, consider the nearest record to its left that is larger than x. Actually the record to the left that is larger is the first record that is greater than x? Because records are increasing, the first record to the left that is > x is the next record after x? Let's think: scanning left to right, we encounter records. For a non-record element, it is not greater than the current maximum, so its value is less than the current maximum (which is the most recent record). That record is larger and to the left. So the nearest larger element to the left is not necessarily a record? Could be a non-record larger than x but smaller than the record? For instance, sequence: [5,4,2,3]. Here records:5 only? Actually 5 is record, 4<5 not record, 2<5, 3<5 not record. For element 3, the nearest larger to left is 5? But there is 4 between them, and 4>3, but 4 is not a record. However, 4 might get killed before it kills 3. So the killer of 3 might be 5 after 4 is removed. So the relevant larger element is the next surviving larger element to the left after eliminations, which is the next record to the left? Because all non-records eventually die, leaving only records. So indeed, the killer of a non-record x will be the record that is the first record to its left (i.e., the most recent record). But is that always true? Let's test: In [5,4,2,3], records:5. So killer of 3 should be 5. Simulation: Step1:5>4 kills4;4>2 kills2;2<3 no. Survivors:5,2,3? Wait we need to recalc: positions: 5,4,2,3. Step1 kills: 5 kills 4; 4 kills 2; 2 does not kill 3 because 2<3. So victims:4,2. Survivors:5,3 => [5,3]. Step2:5>3 kills3. So death time of 3 is 2. The nearest record is 5, and indeed it kills 3 at