Logo
(追記) (追記ここまで)

34611번 - Gathering Sharks 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 2048 MB121212100.000%

문제

You are the leader of a swarm of $n$ sharks living in a one-dimensional ocean. The sharks are positioned from left to right, with each adjacent pair separated by a distance of one unit.

As the leader, you want all the sharks to gather at a common point to form a single group. Initially, no two sharks belong to the same group; for each $i = 1, \dots , n,ドル the $i$-th shark from the left forms its own group, uniquely numbered $a_i,ドル consisting of only itself.

To achieve your goal, you can command the sharks to perform the following actions $n − 1$ times.

  1. You shout out an integer $b$ that meets both conditions:
    • There exists a group numbered $b$.
    • There exists at least one group numbered strictly smaller than $b$.
  2. Afterward, letting $c$ be the largest existing group number strictly smaller than $b,ドル all the sharks in the group numbered $b$ simultaneously move to the position of the group numbered $c,ドル and the two groups merge.
  3. The merged group is numbered $b,ドル and the group numbered $c$ ceases to exist.

All sharks move at a constant speed of one unit distance per unit time. Commands must be executed sequentially, with no overlap in execution. Once a command is completed, the next one can begin immediately.

Compute the minimum time required for all the sharks to gather at a common point by commanding the sharks $n − 1$ times optimally.

입력

The first line of input contains an integer $n$ (2ドル ≤ n ≤ 500$). The second line contains $n$ pairwise distinct integers $a_1, a_2, \dots , a_n$ (1ドル ≤ a_i ≤ n$).

출력

Output the minimum time required for all the sharks to gather at a common point.

제한

예제 입력 1

4
3 2 4 1

예제 출력 1

4

You can command the sharks to perform the following actions:

  1. You shout out 3ドル$. The leftmost shark moves to the position of the second-leftmost shark, and they form a group numbered 3ドル$. This takes 1ドル$ unit of time.
  2. You shout out 4ドル$. The second rightmost shark moves to the position of the group numbered 3ドル,ドル and they form a group numbered 4ドル$. This takes 1ドル$ unit of time.
  3. You shout out 4ドル$. The sharks in the group numbered 4ドル$ move to the rightmost position, forming a group of four sharks. This takes 2ドル$ units of time.

The total time is 1ドル + 1 + 2 = 4$. It can be shown that 4ドル$ units of time is optimal.

예제 입력 2

9
1 2 4 5 7 8 3 6 9

예제 출력 2

17

노트

출처

ICPC > Regionals > Asia Pacific > Asia Pacific Championship > The 2025 ICPC Asia Pacific Championship J번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /