HDU4268 Alice and Bob

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4268

 

比赛时一直想着暴力。囧不囧~~

后来小小学习了一下set。

后面把set学透后再来解释解释回味回味这段AC的代码。

 

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<set>
 4 
 5 using namespace std;
 6 set<int> s; 
 7 struct Node{
 8     int h,w;
 9 }alic[100010],bob[100010];
10 
11 inline int cmp(Node a,Node b){
12     if(a.h==b.h) return a.w<b.w;
13     return a.h<b.h;
14 }
15 
16 int main(){
17     int t,n,i,tot,k;
18     scanf("%d",&t);
19     while(t--){
20         s.clear();
21         scanf("%d",&n);
22         for(i=0;i<n;i++) scanf("%d%d",&alic[i].h,&alic[i].w);
23         for(i=0;i<n;i++) scanf("%d%d",&bob[i].h,&bob[i].w);
24         sort(alic,alic+n,cmp);
25         sort(bob,bob+n,cmp);
26         for(i=0,k=0,tot=0;i<n;i++){
27             while(k<n && bob[k].h<=alic[i].h) s.insert(bob[k++].w);
28             //把可能被此alic[i].h覆盖的都加进去,加进去的是宽 
29             if(s.size()){
30                 set<int>::iterator th=s.lower_bound(alic[i].w);//像是建立一个指针的样子 
31                 if(th==s.end()) th--;//这两行暂时不太好解释 
32                 if(th!=s.begin() && *th>alic[i].w) th--;
33                 if(*th <= alic[i].w) s.erase(th),tot++;
34             }
35         }
36         printf("%d\n",tot);
37     }
38     return 0;
39 }

 

 

选留下来,学习set。还有multiset .

posted @ 2012-09-08 21:51  舞步い嫣语逝爱醉灵魂  阅读(93)  评论(0)    收藏  举报