Блог пользователя rahul_1234

Автор rahul_1234, история, 9 лет назад, По-английски

You are given a number of points on the XY-plane, [(x0,y0),(x1,y1),(x2,y2),...]. A point (xi,yi) is dominant to another point (xj,yj) iff xi>xj and yi>yj. Calculate all pairs of points such that one dominates the other.

A time complexity less then O(n*n) was required.

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
9 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

You can solve it in O(nlogn). First, you sort points by x-coordinate value. And you just do (i can't call it dp) partial sum. What does it mean is, (remember x values are sorted) if x[i]>x[i-1] than d[with value x[i]]=d[with value x[i-1]]+1 where d is arrays where we can count number of points where point i is greater than point j by x coordinate. That takes O(nlogn). Now do the same for y-axis but instead of array d you can make new array d2 (O(nlogn). We did for x-axis and y-axis now we need for both. Easily you can add to 'sum' min(d[i],d2[i]). If you didn't understand something comment. If you want the code comment. UPD: Total time complexity O(nlong)+O(nlogn)=O(nlogn)