Heights of students
Practice
3.5 (2 votes)
Advanced data structures
Binary indexed trees
Data structures
Fenwick tree
Hard
Problem
16% Success 3886 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

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 \(N^{th}\) 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
\(1 \le T \le 5\\ 1 \le N \le 10^5\\ 1 \le height \le 10^{10}\)

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
1 votes
Tags:
Advanced Data StructuresData StructuresDepth First SearchFenwick TreeFenwick treeHardOpentreap
Points:50
4 votes
Tags:
ApprovedCombinatoricsData StructuresFenwick TreeFenwick treeHardOpenTrees
Points:50
2 votes
Tags:
AlgorithmsApprovedBit ManipulationFenwick treeHardOpenTrees