給定 n 個點 (x, y)。若存在另一點 (x', y') 滿足 x' ≥ x 且 y' ≥ y(且不全等),則該點被「支配」。求**不被任何其他點支配**的點的個數(即極大點 / 帕累托前沿 / 猴王)。
支配關係對應矩形區域:點 A 支配點 B ⇔ B 落在以 A 為右上角、原點為左下角的矩形內。所有極大點連成一條從右上到左下的「階梯線」。
maxY(初始 -∞)。y > maxY,則該點不被支配,計數++,更新 maxY = y;否則被支配,跳過。#include <bits/stdc++.h> using namespace std; struct Point { int x, y; }; bool cmp(Point a, Point b) { if (a.x != b.x) return a.x > b.x; return a.y > b.y; } int main() { int n; while (cin >> n) { vector<Point> p(n); for (int i = 0; i < n; ++i) cin >> p[i].x >> p[i].y; sort(p.begin(), p.end(), cmp); int ans = 0, maxY = -1; for (auto &pt : p) { if (pt.y > maxY) { ++ans; maxY = pt.y; } } cout << ans << endl; } return 0; }
int 足夠;若超大用 long long。