[CSP-S 2021]廊桥分配 题解
Preface
这道题当时在考场上就大致想清楚了做法,考场下写了个巨丑无比的树状数组+二分+线段树+ STL set
,我都不知道我是怎么写下来的。
现在回看这道题,发现真的不难QAQ,当时太菜了。当然,现在也菜。
Analysis
注意:这道题的“先到先得”是让我们按照飞机到达的时间升序排序。
首先发现,国内和国外可以分开处理,最后枚举统计。
这就要求我们对国内和国外求出分配 个廊桥时的飞机数。
观察到一个性质:如果分配 个廊桥时飞机 有位置,则分配 个廊桥时飞机仍有位置。
观察一下样例的那张图,手玩一下样例,不难发现这个性质。
那么我们只需要求出每个飞机 所需的最小廊桥数,再用前缀和统计即可。
不妨把廊桥按照 标号,用两个优先队列维护空闲和非空闲的廊桥,设为 。
当遍历到一个新飞机,弹出 中飞走的飞机,把这些廊桥放入 ,再从 取出最小的廊桥。
国内国外的飞机都处理一遍,用前缀和维护一下,然后枚举即可。
时间复杂度 。
Code
// Problem: #3542. 「CSP-S 2021」廊桥分配 // Contest: LibreOJ // URL: https://loj.ac/p/3542 // Memory Limit: 512 MB // Time Limit: 1000 ms // // Powered by CP Editor (https://cpeditor.org) #include <bits/stdc++.h> #define mp make_pair #define fir first #define sec second using namespace std; const int maxn = 1e5 + 5; typedef pair<int,int> pii; struct node { int x,y; node() { x = y = 0; } }a[maxn],b[maxn]; int n,m,k; int sum1[maxn],sum2[maxn]; priority_queue<pii,vector<pii>,greater<pii> > q; priority_queue<int,vector<int>,greater<int> > s; int main() { freopen("airport.in","r",stdin); freopen("airport.out","w",stdout); scanf("%d %d %d",&n,&m,&k); for(int i = 1;i <= m;++ i) { scanf("%d %d",&a[i].x,&a[i].y); } sort(a + 1 , a + 1 + m , [&](const node& p,const node& q) { return p.x < q.x; }); for(int i = 1;i <= k;++ i) { scanf("%d %d",&b[i].x,&b[i].y); } sort(b + 1 , b + 1 + k , [&](const node& p,const node& q) { return p.x < q.x; }); while(!q.empty())q.pop(); while(!s.empty())s.pop(); for(int i = 1;i <= m;++ i)s.push(i); for(int i = 1;i <= m;++ i) { for(;!q.empty()&&q.top().fir < a[i].x;q.pop())s.push(q.top().sec); int ans = s.top(); s.pop(); ++ sum1[ans]; q.push(mp(a[i].y , ans)); } while(!q.empty())q.pop(); while(!s.empty())s.pop(); for(int i = 1;i <= k;++ i)s.push(i); for(int i = 1;i <= k;++ i) { for(;!q.empty()&&q.top().fir < b[i].x;q.pop())s.push(q.top().sec); int ans = s.top(); s.pop(); ++ sum2[ans]; q.push(mp(b[i].y , ans)); } for(int i = 1;i <= n;++ i)sum1[i] += sum1[i - 1],sum2[i] += sum2[i - 1]; int ans = 0; for(int i = 0;i <= n;++ i)ans = max(ans , sum1[i] + sum2[n - i]); printf("%d\n",ans); return 0; }
完结撒花✿✿ヽ(°▽°)ノ✿
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人