bzoj#P3927. [NEERC2014] Improvements

[NEERC2014] Improvements

Description

Son Halo owns nn spaceships numbered from 11 to nn and a space station. They are initially placed on one line with the space station so the spaceship ii is positioned xix_i meters from the station and all ships are on the same side from the station (xi>0)(x_i>0). All xix_i are distinct. Station is considered to have number 00 and x0x_0 is considered to be equal to 00.

Every two spaceships with consequent numbers are connected by a rope, and the first one is connected to the station. The rope number ii (for 1in1\le i\le n) connects ships ii and i1i-1. Note, that the rope number 11 connects the first ship to the station.

Son Halo considers that the rope ii and the rope jj intersect when the segments [ximin,ximax][x_i^{\min},x_i^{\max}] and [xjmin,xjmax][x_j^{\min},x_j^{\max}] have common internal point but neither one of them is completely contained in the other, where $x_k^{\min}=\min\{x_{k-1},x_k\},x_k^{\max}=\max\{x_{k-1},x_k\}$. That is:

$$\begin{cases} x_i^{\min}< x_j^{\min}\land x_j^{\min}< x_i^{\max}\land x_i^{\max}< x_j^{\max}\\ x_j^{\min}< x_i^{\min}\land x_i^{\min}< x_j^{\max}\land x_j^{\max}< x_i^{\max} \end{cases} $$

Son Halo wants to rearrange spaceships in such a way, that there are no rope intersections. Because he is lazy, he wants to rearrange the ships in such a way, that the total number of ships that remain at their original position xix_i is maximal. However, ships can occupy any real positions xix_i after rearrangement.

Your task is to figure out what is the maximal number of ships that can remain at their initial positions.

Input Format

The first line of the input file contains nn - the number of ships. The following line contains nn distinct integers xix_i - the initial positions of the spaceships.

Output Format

The output file must contain one integer - the maximal number of ships that can remain at their initial positions in the solution of this problem.

4
1 3 2 4
3
4
1 4 2 3
4

Limit and Hint

For 100%100\% of testcases, 1n2×1051\le n\le 2\times 10^5, 1xin1\le x_i\le n.

In the first sample Son Halo can remove the second spaceship in the position between the first and the third to solve the problem while keeping 33 other ships at their initial positions.
In the second sample there are no rope intersections, so all 44 ships can be left at their initial positions.

Source

ACM ICPC 2014-2015, NorthEastern European Regional Contest, Problem I