CF362C.Insertion Sort

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

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 aa of size nn in the non-decreasing order.

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 00 to n1n-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 aa of size nn in the non-decreasing order.

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 00 to n1n-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 nn ( 2<=n<=50002<=n<=5000 ) — the length of the permutation. The second line contains nn different integers from 00 to n1n-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)(0,3) and (0,4)(0,4) .

In the second sample the appropriate pairs are (0,4)(0,4) , (1,4)(1,4) , (2,4)(2,4) and (3,4)(3,4) .

首页