아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Wireless Communication Network

시간 제한5초메모리 제한1024 MB

요약
직선 위에 서로 다른 높이로 놓인 기지국들이 인접한 트리를 각 트리에서 가장 높은 정상끼리 연결해 병합될 때, 만들어질 수 있는 트리 지름의 최댓값을 구한다.
난이도

보통10점 중 7점

유형
트리, 그리디, 구현
정답자
아직 제출이 없습니다

문제

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.

Obtained after some repetitions of step 2 with a certain series of choices. There are three trees, T_1T\_1, T_2T\_2, and T_3T\_3. Here, the station h forms the tree T_3T\_3 consisting of only the station hh.
Obtained by linking stations gg and hh in step 2. Trees T_2T\_2 and T_3T\_3, with top summits gg and hh respectively, are adjacent.
Obtained by linking stations bb and hh in the next step 2, merging two adjacent trees. Now all the stations form a single tree.

Figure D.1. The tree formation for Sample 1

입력

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

\begin{align\*}& n \\\ & h\_1 \\\ & \vdots \\\ & h\_n \end{align\*}

Here, nn is the number of communication stations (3≤n≤1063 ≤ n ≤ 10^6), and h_ih\_i is an integer representing the altitude of the ii-th station (1≤h_i≤n1 ≤ h\_i ≤ n). The altitudes of the stations are distinct, that is, h_i≠h_jh\_i \ne h\_j if i≠ji \ne j.

출력

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

예제2

  1. 예제 1

    입력
    8
    1
    8
    2
    3
    5
    4
    6
    7
    
    예상 출력
    6
    
  2. 예제 2

    입력
    4
    1
    2
    3
    4
    
    예상 출력
    3