#P5414. [YNOI2019] 排序
[YNOI2019] 排序
Problem Description
To sort a sequence , we can move from the beginning to the end. However, the cost of this operation is , which is not optimal. The optimal way is to move the consecutive segment to the front of . In this case, the total cost is , which is smaller than the previous cost .
Your task is: for a given sequence, output the minimum cost to sort this sequence.
Input Format
Each input file contains multiple test cases.
The first line contains a positive integer , which represents the number of test cases in the input file.
Then follow test cases, each in the following format:
Each test case contains lines.
The first line contains a positive integer , which represents the number of elements in the sequence, where .
The second line contains integers separated by a single space, representing the elements of the sequence, where .
Output Format
The output file contains lines, each corresponding to the answer for one test case, i.e., the minimum cost to sort the sequence.
1
4
7 1 2 3
6
Hint
Constraints:
For of the testdata: , .
For of the testdata: , .
For of the testdata: , , .
Translated by ChatGPT 5