#P17136. [KOI 2026 #1] 数列排序

    ID: 19479 远端评测题 1000ms 1024MiB 尝试: 0 已通过: 0 显示难度普及+/提高− 上传者: 标签>贪心排序2026KOI(韩国)

[KOI 2026 #1] 数列排序

Problem Description

You are given a sequence A=[A1,A2,…,AN]A = [A_1, A_2, \ldots, A_N] of length NN. You may perform the following operation any number of times, possibly 00 times:

  1. Choose a positive integer xx.
  2. From sequence AA, take all elements with values not greater than xx, keeping their relative order from the original sequence, forming a subsequence BB.
  3. From sequence AA, take all elements with values greater than xx, keeping their relative order from the original sequence, forming a subsequence CC.
  4. Replace the original sequence AA with the sequence obtained by concatenating BB and CC in order, i.e., B+CB+C.

Write a program to compute the minimum number of operations needed to sort sequence AA in non-decreasing order, i.e., to satisfy A1≤A2≤⋯≤ANA_1 \le A_2 \le \cdots \le A_N.

It can be proven that for all inputs satisfying the constraints, the given sequence can always be sorted into non-decreasing order using the operations above.

Input Format

The first line contains an integer NN.

The second line contains NN integers A1,A2,…,ANA_1,A_2,\ldots,A_N, separated by spaces.

Output Format

Output a single integer on the first line, indicating the minimum number of operations required to sort sequence AA in non-decreasing order.

6
3 4 5 1 2 6
1
9
1 5 9 9 5 1 1 5 9
2

Hint

Sample Explanation 1

You can sort sequence AA into non-decreasing order using 11 operation as follows.

  1. Let x=2x=2. Keeping the original relative order, extract all elements with values not greater than x=2x=2, obtaining B:=[1,2]B:=[1,2]. Keeping the original relative order, extract all elements with values greater than x=2x=2, obtaining C:=[3,4,5,6]C:=[3,4,5,6]. Therefore, sequence AA is replaced by B+C=[1,2,3,4,5,6]B+C=[1,2,3,4,5,6].

Sample Explanation 2

You can sort sequence AA into non-decreasing order using 22 operations as follows.

  1. Let x=3x=3. Keeping the original relative order, extract all elements with values not greater than x=3x=3, obtaining B:=[1,1,1]B:=[1,1,1]. Keeping the original relative order, extract all elements with values greater than x=3x=3, obtaining C:=[5,9,9,5,5,9]C:=[5,9,9,5,5,9]. Therefore, sequence AA is replaced by B+C=[1,1,1,5,9,9,5,5,9]B+C=[1,1,1,5,9,9,5,5,9].
  2. Let x=7x=7. Keeping the original relative order, extract all elements with values not greater than x=7x=7, obtaining B:=[1,1,1,5,5,5]B:=[1,1,1,5,5,5]. Keeping the original relative order, extract all elements with values greater than x=7x=7, obtaining C:=[9,9,9]C:=[9,9,9]. Therefore, sequence AA is replaced by B+C=[1,1,1,5,5,5,9,9,9]B+C=[1,1,1,5,5,5,9,9,9].

It can be proven that it is impossible to sort sequence AA into non-decreasing order using fewer than 22 operations.

Constraints

  • All numbers given in the input are integers.
  • 1≤N≤300 0001 \le N \le 300\,000.
  • For each integer ii (1≤i≤N1 \le i \le N), 1≤Ai≤N1 \le A_i \le N.

Subtasks

  1. (66 points) For each integer ii (1≤i≤N1 \le i \le N), Ai≤2A_i \le 2.
  2. (1515 points) N≤15N \le 15.
  3. (2323 points) N≤100N \le 100.
  4. (2727 points) N≤750N \le 750.
  5. (3333 points) For any integers i,ji,j (1≤i<j≤N1 \le i<j \le N), Ai≠AjA_i \ne A_j.
  6. (4646 points) No additional constraints.

Translation completed by ChatGPT-5.6.

Translated by ChatGPT 5