## Editorial for COCI '14 Contest 6 #4 Kratki

**only**when stuck, and

**not to copy-paste code from it**. Please be respectful to the problem author and editorialist.

**Submitting an official solution before solving the problem yourself is a bannable offence.**

The key to solving this task is noticing that in any sequence of length there has to be a monotone subsequence of length at least . Why is that so?

Let's try and prove it.

Let's imagine that we have a set of stacks stacked from left to right, with the feature that the numbers are ascending as we are getting to the top of the stack. Now we do the following algorithm: go through the numbers in the array from left to right, pushing each number to the top of the first stack that we can from the left so that it still holds that the numbers on all stacks are ascending.

Let's observe the first case: after this algorithm, we have less than or equal to nonempty stacks. In that case, there has to be a stack with more than or equal to numbers on it, and it clearly follows that the number of total numbers on stacks is precisely . In this case, we have shown that at least an ascending subsequence is of the length .

Let's now observe the second case: after this algorithm, we have more than nonempty stacks. Let's observe the element on top of each stack. We notice that the numbers on top of the stacks are descending, looking from left to right. In the case that they are not descending, it would mean that we hadn't complied to our construction features during the execution of this algorithm, because we pushed to the top of the first stack that we could so that the next number is larger than the last on the stack, and it is the first stack that we could push to. Having that in mind, we have a descending subsequence of length at least that consists of numbers on top of all stacks.

We have completed the proof. There is always a monotone subsequence of length at least .

After this, it is fairly simple to see that the next construction gives a monotone sequence of required length , assuming that , otherwise such a sequence doesn't exist.

Let the sequence be in the form of .

Each subsequence of length has an ascending subsequence of length , and because there are such sequences, because , a descending sequence of length larger than does not exist.

## Comments