A14993 | PermutationForces
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You have a permutation $p$ of integers from $1$ to $n$ .
You have a strength of $s$ and will perform the following operation some times:
- Choose an index $i$ such that $1 \leq i \leq |p|$ and $|i-p_i| \leq s$ .
- For all $j$ such that $1 \leq j \leq |p|$ and $p_i<p_j$ , update $p_j$ to $p_j-1$ .
- Delete the $i$ -th element from $p$ . Formally, update $p$ to $[p_1,\ldots,p_{i-1},p_{i+1},\ldots,p_n]$ .
It can be shown that no matter what $i$ you have chosen, $p$ will be a permutation of integers from $1$ to $|p|$ after all operations.
You want to be able to transform $p$ into the empty permutation. Find the minimum strength $s$ that will allow you to do so.
You have a strength of $s$ and will perform the following operation some times:
- Choose an index $i$ such that $1 \leq i \leq |p|$ and $|i-p_i| \leq s$ .
- For all $j$ such that $1 \leq j \leq |p|$ and $p_i<p_j$ , update $p_j$ to $p_j-1$ .
- Delete the $i$ -th element from $p$ . Formally, update $p$ to $[p_1,\ldots,p_{i-1},p_{i+1},\ldots,p_n]$ .
It can be shown that no matter what $i$ you have chosen, $p$ will be a permutation of integers from $1$ to $|p|$ after all operations.
You want to be able to transform $p$ into the empty permutation. Find the minimum strength $s$ that will allow you to do so.
输入格式
The first line of input contains a single integer $n$ ( $1 \leq n \leq 5 \cdot 10^5$ ) — the length of the permutation $p$ .
The second line of input conatains $n$ integers $p_1, p_2, \ldots, p_n$ ( $1 \leq p_i \leq n$ ) — the elements of the permutation $p$ .
It is guaranteed that all elements in $p$ are distinct.
The second line of input conatains $n$ integers $p_1, p_2, \ldots, p_n$ ( $1 \leq p_i \leq n$ ) — the elements of the permutation $p$ .
It is guaranteed that all elements in $p$ are distinct.
输出格式
Print the minimum strength $s$ 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 $s$ required is $1$ .
Here is how we can transform $p$ into the empty permutation with $s=1$ :
- In the first move, you can only choose $i=2$ as choosing any other value of $i$ will result in $|i-p_i| \leq s$ being false. With $i=2$ , $p$ will be changed to $[2,1]$ .
- In the second move, you choose $i=1$ , then $p$ will be changed to $[1]$ .
- In the third move, you choose $i=1$ , then $p$ will be changed to $[~]$ .
It can be shown that with $s=0$ , it is impossible to transform $p$ into the empty permutation.
Here is how we can transform $p$ into the empty permutation with $s=1$ :
- In the first move, you can only choose $i=2$ as choosing any other value of $i$ will result in $|i-p_i| \leq s$ being false. With $i=2$ , $p$ will be changed to $[2,1]$ .
- In the second move, you choose $i=1$ , then $p$ will be changed to $[1]$ .
- In the third move, you choose $i=1$ , then $p$ will be changed to $[~]$ .
It can be shown that with $s=0$ , it is impossible to transform $p$ into the empty permutation.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted