公式

C - Not Covered Points 解説 by en_translator


Point \(i\) satisfies the condition if and only if, for all \(j\neq i\), if \(X_j < X_i\) then \(Y_j > Y_i\).

Thus, it suffices to count, when the \(N\) points are sorted in ascending order of \(X\), the number of indices \(i\) with \(1\le i\le N\) such that \(Y_j > Y_i\) for all \(1\le j < i\).

These can be counted by the following algorithm:

  • Sort \((X_i,Y_i)\) in ascending order of \(X_i\).
  • Let \(\text{ans}=0\) and \(Y_{\text{min}}=N+1\), which will store the number of indices \(i\) to be counted, and the minimum value of \(Y_j\) for \(j < i\).
  • For \(i=1,2,\ldots,N\), do the following:
    • If \(Y_{\text{min}} > Y_i\), add \(1\) to \(\text{ans}\).
    • Set \(Y_{\text{min}}\) to \(\min(Y_{\text{min}},Y_i)\).
  • Now \(\text{ans}\) is the sought answer.

The problem can be solved by properly implementing the algorithm above. The computational complexity is \(O(N\log N)\).

Sample code (Python3)

import sys

input = sys.stdin.readline
n = int(input())
xy = []
for i in range(n):
    x, y = map(int, input().split())
    xy.append((x - 1, y - 1))
xy.sort()
min_val = n
ans = 0
for x, y in xy:
    min_val = min(min_val, y)
    ans += min_val == y
print(ans)

投稿日時:
最終更新: