logo AlgoBeat OnlineJudge
登录 注册

#214971. [ICPC 2021 Yokohama R] Wireless Communication Network

内存限制:1024 MiB 时间限制:5000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

We are setting up a wireless communication network in a mountain range. Communication stations are to be located on the summits. The summits are on a straight line and have different altitudes.

To minimize the cost, our communication network should have a tree structure, which is a connected graph with the least number of edges. The structure of the network, that is, which stations to communicate with which other stations, should be decided in advance.

We construct the communication network as follows.

  1. Initially, each station forms a tree consisting of only that station.
  2. In each step, we merge two trees that are adjacent by making a link between two stations in different trees. Two trees are called adjacent when all the summits in between a summit in one tree and a summit in the other belong to one of these two trees. Stations to link are those on the highest summits of the two trees; they are uniquely determined because the altitudes of the summits are distinct.
  3. Repeat the step 2 until all the stations are connected, directly or indirectly.

Figure D.1 depicts an example of the tree formation for Sample 1.

When a station detects an emergency event, an alert message should be broadcast to all the stations. On receiving a message, each station relays the message to all the stations with direct links, except for one from which it has come. The diameter of the network, that is, how many hops are sufficient to distribute messages initiated at any stations, depends on the choice of the two trees in the step 2 above.

To estimate the worst case delay of broadcast, we want to find the largest diameter of the network possibly constructed by the above procedure.

:::align{center}

Figure D.1. The tree formation for Sample 1 :::

Obtained after some repetitions of step 2 with a certain series of choices. There are three trees, , , and . Here, the station forms the tree consisting of only the station .

Obtained by linking stations and in step 2. Trees and , with top summits and respectively, are adjacent.

Obtained by linking stations and in the next step 2, merging two adjacent trees. Now all the stations form a single tree.

输入格式

The input consists of a single test case of the following format.

Here, is the number of communication stations (), and is an integer representing the altitude of the -th station (). The altitudes of the stations are distinct, that is, if .

输出格式

Output in a line a single integer representing the largest possible diameter of the tree.

样例

样例输入 1

8
1
8
2
3
5
4
6
7

样例输出 1

6

样例输入 2

4
1
2
3
4

样例输出 2

3