您的位置:首页 > 其它

Problem I. Improvements

2015-10-27 19:07 369 查看

题目描述:

Problem I. Improvements

Input file: improvements.in

Output file: improvements.out

Son Halo owns n spaceships numbered from 1 to n and a space station. They are initially placed on one

line with the space station so the spaceship i is positioned xi meters from the station and all ships are

on the same side from the station (xi > 0). All xi are distinct. Station is considered to have number 0

and x0 is considered to be equal to 0.

Every two spaceships with consequent numbers are connected by a rope, and the first one is connected

to the station. The rope number i (for 1 ≤ i ≤ n) connects ships i and i−1. Note, that the rope number

1 connects the first ship to the station.

Son Halo considers that the rope i and the rope j intersect when the segments [x

min

i

, xmax

i

] and

[x

min

j

, xmax

j

] have common internal point but neither one of them is completely contained in the other,

where x

min

k = min(xk−1, xk), x

max

k = max(xk−1, xk). That is:

[

x

min

i < xmin

j & x

min

j < xmax

i & x

max

i < xmax

j

x

min

j < xmin

i & x

min

i < xmax

j & x

max

j < xmax

i

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 xi

is maximal. All the ships must stay on the same side of the station and at different

positions xi after rearrangement. However, ships can occupy any real positions xi after rearrangement.

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

Input

The first line of the input file contains n (1 ≤ n ≤ 200 000) — the number of ships. The following line

contains n distinct integers xi (1 ≤ xi ≤ n) — the initial positions of the spaceships.

Output

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.

Sample input and output

improvements.in improvements.out

4

1 3 2 4

3

4

1 4 2 3

4

Note

In the first sample Son Halo can move the second spaceship in the position between the first and the

third to solve the problem while keeping 3 other ships at their initial positions.

In the second sample there are no rope intersections, so all 4 ships can be left at their initial positions.

题解:

当时死活想不出来怎么做.多总结点.

最关键的是,想合法的情况张什么样子.而不是一直去想贪心的调整.因为我们能猜到大概会借助最长上升子序列来解决最终问题,我们应该只关注满足条件的形态. 其中一种想法是,看i-1 和 i 之间能够有什么. 如果出现了一个比i-1小的数,那么数k要求k-1一定在中间,一直产生连锁,会发现0也需要,就推出矛盾. 而i-1 和 i之间有大于i的是可以的. 也可以打标来观察规律,实在是很重要的一种方法.

最终推出来合法答案是:前一段单调上升,后一段单调下降.

重点:

想到用lis的时候,我们不关注怎么调整,关注最终结果的规律.

看给出的稀奇古怪的条件会约束成一个什么样的结果 打标来看 或者枚举两个元素来看都是很好的方法.

代码:

//代码没有写= =
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: