Heights of students

3.5

2 votes
Advanced Data Structures, Binary Indexed Trees, Data Structures, Fenwick Tree, Hard
Problem

All N students of a class are standing on a straight line behind one other student. For example, student 2 is standing behind student 1, student 3 is standing behind student 2, and so on until the Nth student.
The happiness level of a student is determined by the number of students standing before him whose height is strictly greater than him. The total happiness of the class is the sum of the happiness level of all students.
Now you can help minimize this sadness. Your task is to minimize the happiness level of the class. 
Note: You can select one student from anywhere in the line and place him at the end of the line.

Input format

  • First line: An integer T denoting the number of test cases
  • For each test case:
    • First line: An integer N denoting the number of students in a class
    • Next line: N space-separated integers denoting the height of each student

Output format
For each test case, print the minimum total happiness that can be achived.

Constraints
1T51N1051height1010

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Place the first student at end of line
so the new line becomes
1 3 2 4

clearly total sadness level = ( 0 + 0 + 1 + 0 ) = 1 .

Editor Image

?