BZOJ 1651 [Usaco2006 Feb]Stall Reservations 专用牛棚:优先队列【线段最大重叠层数】

题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1651

题意:

  给你n个线段[a,b],问你这些线段重叠最多的地方有几层。

 

题解:

  先将线段按左端点a升序排序。

  开一个优先队列q(升序排序),里面存线段的右端点b。

  枚举线段i,然后:

    (1)将q中所有小于a[i]的元素弹出。

    (2)插入b[i]。

    (3)更新ans = max(ans,q.size())

 

AC Code:

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <algorithm>
 5 #include <queue>
 6 #define MAX_N 50005
 7 
 8 using namespace std;
 9 
10 int n;
11 int ans=0;
12 pair<int,int> p[MAX_N];
13 priority_queue<int,vector<int>,greater<int> > q;
14 
15 int main()
16 {
17     cin>>n;
18     for(int i=0;i<n;i++)
19     {
20         cin>>p[i].first>>p[i].second;
21     }
22     sort(p,p+n);
23     for(int i=0;i<n;i++)
24     {
25         while(!q.empty() && q.top()<p[i].first) q.pop();
26         q.push(p[i].second);
27         ans=max(ans,(int)q.size());
28     }
29     cout<<ans<<endl;
30 }

 

posted @ 2017-10-02 18:23  Leohh  阅读(184)  评论(0编辑  收藏  举报