问题
We are given n students with cgpa (college grades) and jee ranks (Rank in admission exam) of every student. For every student, we have to calculate the number of students who have better cgpa but worse jee rank.
(x1,y1), (x2,y2) ...(xi,yi)... (xn,yn)
for each i, we have to calculate no. of j for which xj > xi and yj > yi (worse rank means greater rank.)
I could come up with the following nlogn algorithm- Sort them decreasing cgpa. Now start scanning from left. Maintain the students scanned so far in a balanced binary tree (according to their jee rank). For the next student, just find out the no of students already scanned with greater rank by querying the balanced binary tree.
I don't know how to maintain a balanced bst in which i can return no. of elements less than k in O(logn). We would need to maintain no. of nodes in subtree at each node. But how to do that?
Either help with the above or else provide a different algorithm, perhaps DP.
回答1:
If you don't want to code balanced binary tree, Binary Indexed Tree (aka BIT or Fenwick Tree) is the DS you should be looking at. It can be coded in < 10 easy lines. Here is a blog post I wrote on Fenwick Trees that might help.
来源:https://stackoverflow.com/questions/8047449/number-of-students-with-better-grades-and-lower-jee-rank