#P16920. [JLCPC 2026] 翻转逆序对
[JLCPC 2026] 翻转逆序对
Problem Description
has a permutation of length . You may perform the following operation at most once: choose two integers and (), and reverse the subarray .
The smart wants to test you: what is the maximum number of inversions in the permutation after the operation?
An inversion is defined as a pair of indices such that and .
Input Format
The first line contains an integer (), indicating the number of test cases. Then follow blocks, each describing one test case. For each test case:
- The first line contains an integer (), the length of the permutation.
- The second line contains integers (), representing the given permutation.
The testdata guarantees that .
Output Format
For each test case, output one integer per line, representing the maximum number of inversions.
3
3
2 1 3
5
5 4 3 2 1
6
3 5 1 4 2 6
2
10
10
Hint
In the first test case, reversing the interval yields , which has inversions: and .
In the second test case, you may choose not to perform any operation. The original permutation is , which has inversions. This is the maximum number of inversions achievable by a permutation of length .
In the third test case, reversing the interval yields , which has inversions: , , , , , , , , , and .
Translated by ChatGPT 5