#P10155. [LSOT-2] 基于二分查找与插入的快速排序

[LSOT-2] 基于二分查找与插入的快速排序

Background

Little H cannot sort, but he can do binary search.

So he created a quicksort based on binary search and insertion.

This problem has a solution that supports modifications, but since this is the check-in problem of this contest, the kind problem setter did not add it.

Problem Description

Given a permutation pp. Each time, you may choose a number pip_i. You need to find the smallest jj such that j>ij>i and pj>pip_j>p_i, and insert pip_i before pjp_j. You need to minimize the number of operations needed to make pi=ip_i=i.

If no such jj exists, then you cannot perform an operation.

Input Format

The first line contains an integer nn, representing the length of the permutation.

The next line contains nn integers describing the permutation pp, where the ii-th number is pip_i.

Output Format

Output one integer on a single line, representing the minimum number of operations. If it is impossible to finish sorting, output 1-1.

5
3 1 4 2 5
2

Hint

"This problem uses bundled tests."

  • Subtask 1(20pts):n10\texttt{Subtask 1(20pts):}n\le10.
  • Subtask 2(20pts):\texttt{Subtask 2(20pts):}guarantee pi=ni+1p_i=n-i+1.
  • Subtask 3(20pts):n1000\texttt{Subtask 3(20pts):}n\le1000.
  • Subtask 4(40pts):\texttt{Subtask 4(40pts):}no special properties.

For all testdata, 1n2×1061\le n\le2\times 10^6, and pp is a permutation of 11 to nn.

Translated by ChatGPT 5