A9209 | Insertion Sort
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array $a$ of size $n$ in the non-decreasing order.
```cpp
for (int i = 1; i < n; i = i + 1)
{
int j = i;
while (j > 0 && a[j] < a[j - 1])
{
swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]
j = j - 1;
}
}
```
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from $0$ to $n-1$ . He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
```cpp
for (int i = 1; i < n; i = i + 1)
{
int j = i;
while (j > 0 && a[j] < a[j - 1])
{
swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]
j = j - 1;
}
}
```
Petya uses this algorithm only for sorting of arrays that are permutations of numbers from $0$ to $n-1$ . He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
输入格式
Petya is a beginner programmer. He has already mastered the basics of the C++ language and moved on to learning algorithms. The first algorithm he encountered was insertion sort. Petya has already written the code that implements this algorithm and sorts the given integer zero-indexed array $a$ of size $n$ in the non-decreasing order.
It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
for (int i = 1; i < n; i = i + 1)<br></br>{<br></br> int j = i; <br></br> while (j > 0 && a[j] < a[j - 1])<br></br> {<br></br> swap(a[j], a[j - 1]); // swap elements a[j] and a[j - 1]<br></br> j = j - 1;<br></br> }<br></br>}<br></br>Petya uses this algorithm only for sorting of arrays that are permutations of numbers from $0$ to $n-1$ . He has already chosen the permutation he wants to sort but he first decided to swap some two of its elements. Petya wants to choose these elements in such a way that the number of times the sorting executes function swap, was minimum. Help Petya find out the number of ways in which he can make the swap and fulfill this requirement.It is guaranteed that it's always possible to swap two elements of the input permutation in such a way that the number of swap function calls decreases.
输出格式
The first line contains a single integer $n$ ( $2<=n<=5000$ ) — the length of the permutation. The second line contains $n$ different integers from $0$ to $n-1$ , inclusive — the actual permutation.
输入输出样例
输入 #1
5 4 0 3 1 2
输出 #1
3 2
输入 #2
5 1 2 3 4 0
输出 #2
3 4
In the first sample the appropriate pairs are $(0,3)$ and $(0,4)$ .
In the second sample the appropriate pairs are $(0,4)$ , $(1,4)$ , $(2,4)$ and $(3,4)$ .
In the second sample the appropriate pairs are $(0,4)$ , $(1,4)$ , $(2,4)$ and $(3,4)$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted