uva-11020-平衡树

题目链接https://vjudge.net/problem/UVA-11020

  白书例题,依次给出n个点的坐标,定义一个点为优势点当且仅当这个点的左下方区域不包含任何点(但可以与之完全重合);求每加入一个点之后余下的优势点的数目。

自己想的很复杂,看了解析之后真的佩服他写的代码,很简练;

首先分析得知,一个点如果不是优势点,那么在后面也不可能成为优势点,直接删除就好了;如果是的话,删除那些在他右上方的点;

 1 #include<set>
 2 #include<cstdio>
 3 #include<iostream>
 4 #include<algorithm>
 5 using namespace std;
 6 struct Point
 7 {
 8     int x,y;
 9     bool operator<(const Point& t)const{
10         return x<t.x||(x==t.x&&y<t.y);
11     }
12     bool operator==(const Point& t)const{
13     return x==t.x&&y==t.y;
14     }
15 }P;
16 multiset<Point> S;
17 multiset<Point>::iterator it;
18 int main()
19 {
20     int t,i,j,n;
21     cin>>t;
22     for(int cas=1;cas<=t;++cas)
23     {
24         cin>>n;
25         S.clear();
26         if(cas>1) puts("");
27         printf("Case #%d:\n",cas);
28         for(i=1;i<=n;++i){
29             cin>>P.x>>P.y;
30                 it = S.lower_bound(P);
31                 if(it==S.begin()||(--it)->y>P.y){  //不满足说明不是优势点不必添加进去也不会影响结果
32                     S.insert(P);
33                     it=S.upper_bound(P);
34                     while(it!=S.end()&&it->y>=P.y)S.erase(it++);
35 
36                 } printf("%d\n",S.size());
37             }
38     }
39     return 0;
40 }

 

posted @ 2018-02-01 21:37  *zzq  阅读(234)  评论(0编辑  收藏  举报