#P15501. [ICPC 2025 APC] Gathering Sharks
[ICPC 2025 APC] Gathering Sharks
Description
You are the leader of a swarm of 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 , the -th shark from the left forms its own group, uniquely numbered , consisting of only itself.
To achieve your goal, you can command the sharks to perform the following actions times.
- You shout out an integer that meets both conditions:
- There exists a group numbered .
- There exists at least one group numbered strictly smaller than .
- Afterward, letting be the largest existing group number strictly smaller than , all the sharks in the group numbered simultaneously move to the position of the group numbered , and the two groups merge.
- The merged group is numbered , and the group numbered 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 times optimally.
Input Format
The first line of input contains an integer (). The second line contains pairwise distinct integers ().
Output Format
Output the minimum time required for all the sharks to gather at a common point.
4
3 2 4 1
4
9
1 2 4 5 7 8 3 6 9
17
Hint
Explanation for the sample input/output #1
You can command the sharks to perform the following actions:
- You shout out . The leftmost shark moves to the position of the second-leftmost shark, and they form a group numbered . This takes unit of time.
- You shout out . The second rightmost shark moves to the position of the group numbered , and they form a group numbered . This takes unit of time.
- You shout out . The sharks in the group numbered move to the rightmost position, forming a group of four sharks. This takes units of time.
The total time is . It can be shown that units of time is optimal.
京公网安备 11011102002149号