P15588 [KTSC 2026] 飞扬的松鼠 2 / Flying Squirrel 2
EuphoricStar · · 题解
首先我们将
考虑
考虑用另一种方式描述高度在
于是问题可以变成一个二分图最小权匹配问题:左部点为每一次升高,右部点为所有柱子。左部点
由于所有能被左部点匹配覆盖的柱子集合构成拟阵,所以上述问题可以直接贪心:按
为了判定这个条件,可以使用 Hall 定理,即判断每一个已加入的柱子子集
可以只判定
可以转而求左式的最大值。对于每个 (,),对于每个已加入的柱子 *(位置相同的字符按 (*) 的顺序排列)。即求形如
考虑有
- 满足
T_i \ne -1 的i 构成一个区间; -
-
那么我们先求出一个蓝柱子数量最小的最优解,然后按
那么也就是求这个蓝柱子左边的最长的
但是这样求出来的
时间复杂度
:::info[代码]
#include <bits/stdc++.h>
#ifdef LOCAL
#include "grader.cpp"
#endif
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
using ll = long long;
using ull = unsigned long long;
using db = double;
using ldb = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int maxn = 200100;
const ll inf = 0x3f3f3f3f3f3f3f3fLL;
int n, m, f[maxn], g[maxn], L[maxn], R[maxn], b[maxn], ord[maxn];
ll a[maxn];
struct node {
int c0, c1, c2, c01, c12, c012;
pll x1, x2, mn;
node() {
c0 = c1 = c2 = c01 = c12 = c012 = 0;
x1 = x2 = mn = pll(inf, 0);
}
};
struct seg {
int l, r;
} c[maxn];
inline node operator + (const node &a, const node &b) {
node res;
res.c0 = a.c0 + b.c0;
res.c1 = a.c1 + b.c1;
res.c2 = a.c2 + b.c2;
res.c01 = max(a.c01 + b.c1, a.c0 + b.c01);
res.c12 = max(a.c12 + b.c2, a.c1 + b.c12);
res.c012 = max({a.c0 + b.c012, a.c01 + b.c12, a.c012 + b.c2});
res.mn = min(a.mn, b.mn);
if (a.c0 + b.c01 >= a.c01 + b.c1) {
res.x1 = b.x1;
} else {
res.x1 = min(a.x1, b.mn);
}
if (a.c12 + b.c2 >= a.c1 + b.c12) {
res.x2 = a.x2;
} else {
res.x2 = min(b.x2, a.mn);
}
return res;
}
namespace SGT {
node a[maxn << 2];
int N;
inline void init() {
N = (1 << (__lg(n + 1) + 1));
for (int i = 1; i <= n; ++i) {
a[N + i].c0 = a[N + i].c01 = f[i];
a[N + i].c2 = a[N + i].c12 = g[i];
a[N + i].c012 = max(f[i], g[i]);
}
for (int i = N; i; --i) {
a[i] = a[i << 1] + a[i << 1 | 1];
}
}
inline void update(int x, int y, pll z) {
x += N;
a[x].c1 += y;
a[x].c01 = max(a[x].c0, a[x].c1);
a[x].c12 = max(a[x].c1, a[x].c2);
a[x].c012 = max({a[x].c0, a[x].c1, a[x].c2});
a[x].x1 = a[x].x2 = a[x].mn = z;
while (x >>= 1) {
a[x] = a[x << 1] + a[x << 1 | 1];
}
}
inline node query(int l, int r) {
if (l > r) {
return node();
}
node resl, resr;
for (l += N - 1, r += N + 1; l ^ r ^ 1; l >>= 1, r >>= 1) {
if (!(l & 1)) {
resl = resl + a[l ^ 1];
}
if (r & 1) {
resr = a[r ^ 1] + resr;
}
}
return resl + resr;
}
}
vector<ll> fly(int _h, vector<int> _a, vector<int> _b, vector<int> _l, vector<int> _r) {
n = (int)_a.size();
m = _h;
for (int i = 1; i <= n; ++i) {
a[i] = _a[i - 1];
b[i] = _b[i - 1];
L[i] = _l[i - 1];
R[i] = _r[i - 1];
}
for (int i = n - 1; ~i; --i) {
L[i] = max(L[i], L[i + 1] - 1);
R[i] = min(R[i], R[i + 1]);
}
for (int i = 1; i <= n; ++i) {
L[i] = max(L[i], L[i - 1]);
R[i] = min(R[i], R[i - 1] + 1);
}
if (!(L[n] <= m && m <= R[n])) {
return vector<ll>(m + 1, -1);
}
for (int i = 0; i <= n; ++i) {
R[i] = min(R[i], m);
if (L[i] > R[i]) {
return vector<ll>(m + 1, -1);
}
}
for (int i = 1; i <= n; ++i) {
for (int j = L[i - 1] + 1; j <= L[i]; ++j) {
c[j].r = i;
}
for (int j = R[i - 1] + 1; j <= R[i]; ++j) {
c[j].l = i;
}
}
for (int i = L[n] + 1; i <= m; ++i) {
c[i].r = n;
}
for (int i = 1; i <= m; ++i) {
++f[c[i].r];
++g[c[i].l];
}
SGT::init();
for (int i = 1; i <= n; ++i) {
ord[i] = i;
}
sort(ord + 1, ord + n + 1, [&](const int &i, const int &j) {
return b[i] < b[j] || (b[i] == b[j] && a[i] < a[j]);
});
ll res = 0;
int cnt = 0, ct = 0;
vector<int> vc;
for (int _ = 1; _ <= n; ++_) {
int i = ord[_];
SGT::update(i, 1, b[i] ? pll(inf, 0) : pll(-a[i], i));
if (SGT::a[1].c012 > m) {
SGT::update(i, -1, pll(inf, 0));
if (b[i]) {
vc.pb(i);
}
} else {
++cnt;
ct += b[i];
res += a[i];
}
}
if (cnt != m) {
return vector<ll>(m + 1, -1);
}
vector<ll> ans(m + 1, -1);
ans[ct] = res;
vector<ll> ds;
for (int i : vc) {
node x = SGT::query(1, i - 1), y = SGT::query(i + 1, n);
if (min(x.x1, y.x2).fst == inf) {
continue;
}
SGT::update(i, 1, pll(inf, 0));
pll p = min(x.x1, y.x2);
SGT::update(p.scd, -1, pll(inf, 0));
ds.pb(a[i] + p.fst);
}
sort(ds.begin(), ds.end());
for (ll x : ds) {
++ct;
ans[ct] = ans[ct - 1] + x;
}
return ans;
}
:::