UVA 11020,uva11020
UVA 11020 - Efficient Solutions
題目連結
題意:每個人有兩個屬性值(x, y),對於每一個人(x,y)而言,當有另一個人(x', y'),如果他們的屬性值滿足x' < x, y' <= y或x' <= x, y' < y的話,這個人會失去優勢,每次添加一個人,並輸出當前優勢人個數
思路:由於每個人失去優勢後,不可能再得到優勢,所以失去優勢就可以當成刪去這些點,這樣的話,就可以用一個multiset來維護點集,每次加入一個點,利用lowerbound,upper_bound二分尋找旁邊點的位置來進行判斷和刪點
代碼:
#include <cstdio>#include <cstring>#include <iostream>#include <set>using namespace std;int t, n;struct Point { int x, y; bool operator < (const Point &c) const {if (x == c.x) return y < c.y;return x < c.x; }};multiset<Point> s;multiset<Point>::iterator it;int main() { int cas = 0; cin >> t; while (t--) {s.clear();cin >> n;Point u;cout << "Case #" << ++cas << ":" << endl;while (n--) { cin >> u.x >> u.y; it = s.lower_bound(u); if (it == s.begin() || (--it)->y > u.y) {s.insert(u);it = s.upper_bound(u);while (it != s.end() && it->y >= u.y) s.erase(it++); } cout << s.size() << endl;}if (t) cout << endl; } return 0;}