#P8613. [蓝桥杯 2014 省 B] 小朋友排队
[蓝桥杯 2014 省 B] 小朋友排队
Description
There are kids standing in a line. Now we want to arrange them in non-decreasing order of height (from short to tall), but each operation can only swap two adjacent kids.
Each kid has an "unhappiness" value. At the beginning, every kid’s unhappiness is .
If a kid is asked to swap for the first time, their unhappiness increases by . If they are asked to swap for the second time, their unhappiness increases by (so the total becomes ), and so on. In general, when a kid is asked to swap for the -th time, their unhappiness increases by .
Find the minimum possible total unhappiness (the sum of all kids’ unhappiness values) after making all kids line up from short to tall.
If two kids have the same height, it does not matter which one stands in front of the other.
Input Format
The first line contains an integer , the number of kids.
The second line contains integers , where is the height of the -th kid.
Output Format
Output one line containing one integer, the minimum possible total unhappiness.
3
3 2 1
9
Hint
[Sample Explanation]
First swap the kids with heights and , then swap the kids with heights and , and then swap the kids with heights and . Each kid’s unhappiness is , so the total is .
[Constraints]
For of the testdata, .
For of the testdata, .
For of the testdata, .
For of the testdata, , .
Time limit: 1 second. Memory limit: 256 MB. Lanqiao Cup 2014, the 5th Provincial Contest.
Translated by ChatGPT 5
京公网安备 11011102002149号