CF1672I.PermutationForces

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You have a permutation pp of integers from 11 to nn .

You have a strength of ss and will perform the following operation some times:

  • Choose an index ii such that 1ip1 \leq i \leq |p| and ipis|i-p_i| \leq s .
  • For all jj such that 1jp1 \leq j \leq |p| and pi<pjp_i<p_j , update pjp_j to pj1p_j-1 .
  • Delete the ii -th element from pp . Formally, update pp to [p1,,pi1,pi+1,,pn][p_1,\ldots,p_{i-1},p_{i+1},\ldots,p_n] .

It can be shown that no matter what ii you have chosen, pp will be a permutation of integers from 11 to p|p| after all operations.

You want to be able to transform pp into the empty permutation. Find the minimum strength ss that will allow you to do so.

输入格式

The first line of input contains a single integer nn ( 1n51051 \leq n \leq 5 \cdot 10^5 ) — the length of the permutation pp .

The second line of input conatains nn integers p1,p2,,pnp_1, p_2, \ldots, p_n ( 1pin1 \leq p_i \leq n ) — the elements of the permutation pp .

It is guaranteed that all elements in pp are distinct.

输出格式

Print the minimum strength ss required.

输入输出样例

  • 输入#1

    3
    3 2 1

    输出#1

    1
  • 输入#2

    1
    1

    输出#2

    0
  • 输入#3

    10
    1 8 4 3 7 10 6 5 9 2

    输出#3

    1

说明/提示

In the first test case, the minimum ss required is 11 .

Here is how we can transform pp into the empty permutation with s=1s=1 :

  • In the first move, you can only choose i=2i=2 as choosing any other value of ii will result in ipis|i-p_i| \leq s being false. With i=2i=2 , pp will be changed to [2,1][2,1] .
  • In the second move, you choose i=1i=1 , then pp will be changed to [1][1] .
  • In the third move, you choose i=1i=1 , then pp will be changed to [ ][~] .

It can be shown that with s=0s=0 , it is impossible to transform pp into the empty permutation.

首页