#P15810. [JOI 2013 Final] バブルソート

[JOI 2013 Final] バブルソート

Problem Description

Bubble sort is an algorithm for sorting a sequence. Suppose we want to sort an array AA of length NN in ascending order. Bubble sort checks two adjacent numbers, and if their order is incorrect, it swaps them. This process is done by scanning the array from front to back. That is, if there exists a position with Ai>Ai+1A_i > A_{i+1}, then swap these two numbers, and perform this check once for each ii in the order i=1,2,,N1i = 1, 2, \dots, N-1; this is called one scan. It is known that repeating such scans N1N-1 times will sort the array in ascending order.

The number of swaps in bubble sort for the array AA means the number of integer swaps that occur when applying the above algorithm to AA. (Known bubble sort algorithms and their implementations may differ slightly in loop order, range, termination conditions, etc. However, it is known that when applied to the same array, the number of integer swaps does not change because of these differences.)

For example, the following program is a function written in C that sorts an integer array aa of length nn using bubble sort.

void bubble_sort(int *a, int n) {
    int i, j;
    for (i = 0; i < n - 1; ++i) {
        for (j = 0; j < n - 1; ++j) {
            if (a[j] > a[j + 1]) {
                /* The following 3 lines correspond to one integer swap */
                int x = a[j];
                a[j] = a[j + 1];
                a[j + 1] = x;
            }
        }
    }
}

Task

You are given a sequence AA of length NN. Suppose we obtain a new sequence AA' by swapping two integers at arbitrary positions in AA exactly once. Write a program to find the minimum possible number of swaps in bubble sort for the sequence AA'. (Note that the two integers swapped at the beginning do not have to be adjacent.)

Input Format

Read the following data from standard input.

  • The first line contains an integer NN. NN is the length of the sequence AA.
  • In the next NN lines, the ii-th line (1iN1 \leq i \leq N) contains an integer AiA_i. This represents the ii-th integer of the sequence AA.

Output Format

Output one line to standard output containing one integer, which is the minimum possible number of swaps in bubble sort for the sequence AA'.

5
10
3
6
8
1
0
5
3
1
7
9
5
2
3
1
2
3
1

Hint

Sample Explanation 1

If you swap 1010 at the beginning of the sequence AA with 11 at the end, then the sequence AA' becomes a sorted sequence, and its number of swaps in bubble sort is 00.

Sample Explanation 2

If you swap the third number 77 in the sequence AA with the last number 55, then AA' becomes 3,1,5,9,73, 1, 5, 9, 7. The number of swaps in bubble sort for AA' is 22.

Sample Explanation 3

Even if the sequence AA is already sorted at the beginning, you still must perform one swap when constructing AA'.

Constraints

1N1000001 \leq N \leq 100\,000 the length of the sequence AA
1Ai10000000001 \leq A_i \leq 1\,000\,000\,000 the value of the numbers in the sequence AA

Scoring

In the testdata for scoring, the part worth 10% satisfies N1000N \leq 1000, and for any i,ji, j (1i<jN1 \leq i < j \leq N), we have AiAjA_i \neq A_j.
In the testdata for scoring, the part worth 30% satisfies N5000N \leq 5000, and for any i,ji, j (1i<jN1 \leq i < j \leq N), we have AiAjA_i \neq A_j.
In the testdata for scoring, the part worth 80% satisfies that for any i,ji, j (1i<jN1 \leq i < j \leq N), we have AiAjA_i \neq A_j.

Translated by ChatGPT 5