讲一个挺好写的做法
对于国际和国内分别维护分配x个廊桥时停靠廊桥的飞机数量
显然答案就是
注意到选择飞机的方法是先到先得而非任意分配,因此可以人为给廊桥编号,增加最后一座廊桥时对之前即能分配到廊桥的飞机无影响
考虑第一架能享受新加入廊桥的飞机,显然是当前未分配到廊桥的飞机中到达时间最早的
第二架飞机是当前未分配到廊桥且到达时间晚于第一架离开时间的飞机中到达时间最小的
第三架飞机是当前未分配到廊桥且到达时间晚于第二架离开时间的飞机中到达时间最小的 …
即一个贪心覆盖的过程
需要维护一个数据结构满足删除和查询大于一个数的最小值,set即可
时间复杂度
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<iomanip>
#include<vector>
#include<set>
#include<algorithm>
#define pii pair<int, int>
using namespace std;
const int N = 200005;
int n, m1, m2, ans1[N], ans2[N];
struct h
{
int x, y;
}a[N], b[N];
set<pii>s;
int main()
{
scanf("%d%d%d", &n, &m1, &m2);
for(int i = 1; i <= m1; i++)
{
scanf("%d%d", &a[i].x, &a[i].y);
s.insert(pii(a[i].x, a[i].y));
}
ans1[0] = 0;
for(int i = 1; i <= n; i++)
{
ans1[i] = ans1[i - 1];
if(s.empty())continue;
set<pii>::iterator x = s.begin();
do
{
ans1[i]++;
set<pii>::iterator p = s.lower_bound(pii(x -> second, 0));
s.erase(x); x = p;
}while(x != s.end());
}
s.clear();
for(int i = 1; i <= m2; i++)
{
scanf("%d%d", &b[i].x, &b[i].y);
s.insert(pii(b[i].x, b[i].y));
}
ans2[0] = 0;
for(int i = 1; i <= n; i++)
{
ans2[i] = ans2[i - 1];
if(s.empty())continue;
set<pii>::iterator x = s.begin();
do
{
ans2[i]++;
set<pii>::iterator p = s.lower_bound(pii(x -> second, 0));
s.erase(x); x = p;
}while(x != s.end());
}
int maxn = 0;
for(int i = 0; i <= n; i++)
maxn = max(maxn, ans1[i] + ans2[n - i]);
printf("%d\n", maxn);
return 0;
}