#t144. 排队调整
排队调整
Queue Reordering
Problem Description
During a PE class, students line up in a queue. From front to back, the -th student has height and weight . The current lineup looks uneven, and the teacher wants the students to stand in order of decreasing height, and among students of the same height, in order of decreasing weight. When rearranging the queue, only two adjacent students may be swapped at a time. The teacher wants to know the minimum number of swaps needed to arrange the queue into the target order.
Input Format
The first line contains a positive integer , the number of students.
The following lines each contain two positive integers and , the height and weight of the -th student.
Output Format
Output one line containing one integer: the minimum number of swaps needed.
Sample Input 1
5
1 60
3 70
2 80
4 55
4 50
Sample Input 2
5
4 0
4 0
2 0
3 0
1 0
Sample Output 1
8
Sample Output 2
1
Sample Explanation
For the first sample, the target order is: the student of height and weight stands first, then the student of height and weight , then height weight , then height weight , and finally height weight . Replace each student in the initial queue by his position index in the target queue; the initial sequence becomes . The minimum number of adjacent swaps to sort this sequence equals the number of inversions in it. The sequence has inversions: forms pairs with the following ; forms pairs with ; forms pairs with ; for a total of . Therefore at least swaps are needed.
Constraints
For all test cases, and are guaranteed.