Improvements
Time limit1sMemory limit256 MB
Reposition ships on a line from a station so no two ropes joining consecutive ships cross, keeping as many ships as possible in place.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Combinatorics, Segment tree
- Solved
- No attempts yet
Problem
Son Halo owns spaceships, numbered from 1 to , and one space station. The station and the ships all sit on a single line. Ship is meters from the station, and every is positive, so all ships are on the same side of the station. All are distinct. The station has number 0, and .
Every two ships with consecutive numbers are joined by a rope, and the first ship is joined to the station. Rope (for ) joins ship and ship , so rope 1 joins the first ship to the station.
Write and . Son Halo considers that rope and rope intersect when the segments and have a common interior point and neither segment is completely contained in the other, that is, when one of the following holds.
Son Halo wants to rearrange the ships so that no two ropes intersect. He is lazy, so he wants the number of ships that remain at their original position to be as large as possible. After the rearrangement all ships must still be on the same side of the station and at distinct positions. A ship can be placed at any real position.
Find the largest number of ships that can remain at their initial positions.
Input
The first line contains (), the number of ships. The second line contains distinct integers (), the initial positions of the ships.
Output
Print one integer, the largest number of ships that can remain at their initial positions.
Note
In the first example Son Halo can move the second ship to a position between the first ship and the third ship, and the other 3 ships keep their places. In the second example no two ropes intersect, so all 4 ships can stay where they are.