195 字
1 分钟
题解:UVA11776 Oh Your Royal Greediness!
UVA11776 Oh Your Royal Greediness! 题解
思路
考虑扫描线。
将每个区间的起点和终点分别作为事件,起点标记为开始一个区间,终点标记为结束一个区间。
对所有事件按时间排序,如果时间相同,先处理终点再处理起点,这样就能确保在端点处不重复计数。
从左到右扫描,维护当前活跃区间数,并记录其最大值即为答案。
Code
#include<bits/stdc++.h>using namespace std;#define int long long#define pii pair<int,int>#define fi first#define se secondint n,tot;pii a[2005];signed main() { ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); while (cin>>n && n!=-1) { int p=0; for (int i=1,s,e;i<=n;i++) cin>>s>>e,a[++p]={s,0},a[++p]={e,1}; sort(a+1,a+1+p); int ans=0,cnt=0; for (int i=1;i<=p;i++) { if (a[i].se==0) cnt++; else cnt--; ans=max(ans,cnt); } cout<<"Case "<<++tot<<": "<<ans<<'\n'; } return 0;}题解:UVA11776 Oh Your Royal Greediness!
https://banglee13.github.io/posts/uva11776/分享文章
生成精美分享图或复制链接,与更多人分享本文。
继续阅读
换条路线
从其他文章中稳定抽取
最后更新于 ,距今已过 318 天
部分内容可能已过时