CF1672I.PermutationForces

NOI/NOI+/CTSC

通过率:0%

时间限制:5.00s

内存限制:1024MB

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 1≤i≤∣p∣1 \leq i \leq |p| and ∣i−pi∣≤s|i-p_i| \leq s.
  • For all jj such that 1≤j≤∣p∣1 \leq j \leq |p| and pi<pjp_i \lt p_j, update pjp_j to pj−1p_j-1.
  • Delete the ii-th element from pp. Formally, update pp to [p1,…,pi−1,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.

你有一个 11 到 nn 的整数排列 pp。

你拥有强度 ss,并可执行以下操作若干次:

  • 选择一个下标 ii,满足 1≤i≤∣p∣1 \leq i \leq |p| 且 ∣i−pi∣≤s|i-p_i| \leq s;
  • 对所有满足 1≤j≤∣p∣1 \leq j \leq |p| 且 pi<pjp_i \lt p_j 的 jj,将 pjp_j 更新为 pj−1p_j-1;
  • 从 pp 中删除第 ii 个元素。形式化地,将 pp 更新为 [p1,…,pi−1,pi+1,…,pn][p_1,\ldots,p_{i-1},p_{i+1},\ldots,p_n]。

可以证明:无论你选择哪个 ii,在所有操作完成后,pp 始终是 11 到 ∣p∣|p| 的一个排列。

你希望最终能将 pp 变为一个空排列。求实现该目标所需的最小强度 ss。

输入格式

The first line of input contains a single integer nn (1≤n≤5⋅1051 \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 (1≤pi≤n1 \leq p_i \leq n) — the elements of the permutation pp.

It is guaranteed that all elements in pp are distinct.

输入的第一行包含一个整数 nn(1≤n≤5⋅1051 \leq n \leq 5 \cdot 10^5)—— 表示排列 pp 的长度。

输入的第二行包含 nn 个整数 p1,p2,…,pnp_1, p_2, \ldots, p_n(1≤pi≤n1 \leq p_i \leq n)—— 表示排列 pp 的元素。

保证 pp 中所有元素互不相同。

输出格式

Print the minimum strength ss required.

输出所需的最小强度 ss。

输入输出样例

  • 输入#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 ∣i−pi∣≤s|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.

在第一个测试用例中,所需的最小 ss 为 11。

以下是当 s=1s=1 时,我们将 pp 变换为空排列的过程:

  • 在第一步中,你只能选择 i=2i=2,因为选择其他任意 ii 值都会导致 ∣i−pi∣≤s|i-p_i| \leq s 不成立。当 i=2i=2 时,pp 将变为 [2,1][2,1]。
  • 在第二步中,你选择 i=1i=1,则 pp 将变为 [1][1]。
  • 在第三步中,你选择 i=1i=1,则 pp 将变为 [ ][~]。

可以证明:当 s=0s=0 时,无法将 pp 变换为空排列。

输入解题思路,AI测评打分。不知道怎么写?

首页