P7913 [CSP-S 2021] 廊桥分配 题解
现在的 T1 真是一年比一年有趣啊,总的来说这次的 T1 并不算特别难,真不知道为什么有人说 T3 比 T1 简单的。
考场上我是想到了这道题的正解,100 分妥妥的。
在题解区转了一圈,发现什么样的写法都有,像什么各种 STL 啊,像线段树二分啊。总觉得大家写得都不怎么好看,不容易让人理解。所以我就亲自来写一份题解吧。
由于国内航班和国际航班是完全等价的,所以我们可以分开单独进行完全相同的处理的处理。
我们约定用
为了求
我们可以现将所有的起飞和降落时间在一起进行排序,记录当前时间是降落还是起飞以及飞机的编号。排序后,如果当前时间是降落,则将该飞机停靠在最优廊桥,并记录该飞机所在的廊桥;如果是起飞,则将该飞机所在的廊桥插入优先队列(小根堆)。
最后通过枚举
一切都是很好处理的,代码也很好写,个人感觉比其他题解写的好看。
复杂度:
(代码中有注释)
#include <bits/stdc++.h>
using namespace std;
inline int read()//读入优化
{
int x = 0, f = 1;
char ch = getchar();
while (!isdigit(ch))
{
f = ch != '-';
ch = getchar();
}
while (isdigit(ch))
{
x = (x << 1) + (x << 3) + (ch ^ 48);
ch = getchar();
}
return f ? x : -x;
}
const int N = 2e5 + 10;
int n, m1, m2, t, t1, t2, k1, k2, ans;
int c1[N], c2[N], d1[N], d2[N], f1[N], f2[N];
struct time
{
int t, id;//时间和飞机编号
bool f;//0为降落,1为起飞
bool operator<(const time &x) const//sort需要重载运算符<
{
return t < x.t;//以时间为关键字进行排序
}
} s1[N], s2[N];
priority_queue<int, vector<int>, greater<int>> q1, q2;//注意是小根堆
int main()
{
n = read(), m1 = read(), m2 = read();
for (int i = 1; i <= m1; i++)
{
int l = read(), r = read();
s1[++t1] = {l, i, 0};
s1[++t1] = {r, i, 1};
}
for (int i = 1; i <= m2; i++)
{
int l = read(), r = read();
s2[++t2] = {l, i, 0};
s2[++t2] = {r, i, 1};
}
sort(s1 + 1, s1 + t1 + 1);
sort(s2 + 1, s2 + t2 + 1);
for (int i = 1; i <= t1; i++)
if (!s1[i].f)//若为降落
{
int x;
if (!q1.empty())
{
x = q1.top();
q1.pop();
}
else
x = ++k1;//若堆为空,则新开一个廊桥
d1[s1[i].id] = x;//记录该飞机所在廊桥编号
c1[x]++;//停靠在该廊桥的飞机+1
}
else
q1.push(d1[s1[i].id]);//插入优先队列
for (int i = 1; i <= t2; i++)
if (!s2[i].f)
{
int x;
if (!q2.empty())
{
x = q2.top();
q2.pop();
}
else
x = ++k2;
d2[s2[i].id] = x;
c2[x]++;
}
else
q2.push(d2[s2[i].id]);
for (int i = 1; i <= n; i++)//求前缀和
{
f1[i] = f1[i - 1] + c1[i];
f2[i] = f2[i - 1] + c2[i];
}
for (int i = 0; i <= n; i++)//最后枚举得出答案
ans = max(ans, f1[i] + f2[n - i]);
printf("%d\n", ans);
return 0;
}
最后祝大家NOIP RP++!!!