题解:AT_abc447_g [ABC447G] Div. 1 & Div. 2
官方题解翻译
为简单起见,我们把候选问题简称为“问题”。对于一组问题
- 首先,让
T=\emptyset 。对于S 中的每个问题p ,按照兴趣从大到小的顺序执行以下操作。- 如果
|T| < X 且T 中没有与p 相同流派的问题,则将p 插入T 。
- 如果
定义集合
为方便起见,如果具有成对不同流派的问题少于四个,则在集合中添加感兴趣的虚拟问题
请考虑以下方法:
- 我们尝试
K_{i_4} 的所有候选数,用c 表示。 - 针对
c 的每个值,找出所有带有K_{i_4}=c 的索引i_4 。 - 对于
i_4 的每个值,找出最优的i_3 和答案的最大值。
假设
设
另外,将
- 问题
i_4,i_5 的最大总兴趣,以及i_6 ,使得d,K_{i_4},K_{i_5},K_{i_6} 在i_1 和i_2 (如果是d=K_{i_4} ,则为-\infty )的选择中具有不同的流派。
我们要找到的是
- 事实 1:要找到
M(x) + f(i_4, K_x) 的最大值,只需考虑问题1,2,\dots,i_4-1 中不同流派的前四个有趣问题即可。
这里我们省略了详细的证明,但这是由于对于所有的
“
根据目前的观察结果,我们现在可以对固定的
- 花费
O(N) 时间找到M(x) 的所有值并构建段树。对于每个有K_{i_4} =c 的i_4 ,在O(N\log N) 时间内找出最大答案。
然而,由于我们需要穷举搜索所有的
- 事实 2:对于所有的
x ,当我们扫描c=1,2,\dots,N 时,值M(x) 只变化了O(1) 次。(此外,可以在O(1) 时间内枚举出过渡点)。
我们在此省略证明,但可以通过跟踪根据
因此,我们不需要为每一个
需要注意的是,虽然复杂度为 std::vector 会使常量因子恶化;一般情况下,强烈建议使用 std::array 代替。
示例代码 (C++):
#include <bits/stdc++.h>
#include <atcoder/segtree>
using namespace std;
using ll = long long;
const ll inf = 1LL << 60;
const int SZ = 4;
using S = array<pair<ll, ll>, SZ>;
S op(const S &a, const S &b) {
array<pair<ll, ll>, SZ * 2> c;
copy(a.begin(), a.end(), c.begin());
copy(b.begin(), b.end(), c.begin() + SZ);
sort(c.rbegin(), c.rend());
S res;
int it = 0;
for (int i = 0; i < SZ; i++) {
while (true) {
bool ok = true;
for (int j = 0; j < i; j++) {
if (res[j].second == c[it].second) {
ok = false;
break;
}
}
if (ok) break;
it++;
}
res[i] = c[it++];
}
return res;
}
S e() {
return {
make_pair(-inf, -1),
make_pair(-inf, -2),
make_pair(-inf, -3),
make_pair(-inf, -4)
};
}
S single(ll k, ll a) {
a = max(a, -inf);
S res = e();
res[0] = {a, k};
return res;
}
S add(S s, ll k, ll a) {
for (int i = 0; i < SZ; i++) {
if (s[i].second == k) {
s[i].first = max(s[i].first, a);
sort(s.rbegin(), s.rend());
return s;
}
}
array<pair<ll, ll>, SZ + 1> tmp;
copy(s.begin(), s.end(), tmp.begin());
tmp[SZ] = {a, k};
sort(tmp.rbegin(), tmp.rend());
for (int i = 0; i < SZ; i++) {
s[i] = tmp[i];
}
return s;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<ll> k(n), a(n);
vector<vector<int>> ls(n);
for (int i = 0; i < n; i++) {
cin >> k[i] >> a[i];
--k[i];
ls[k[i]].push_back(i);
}
vector<S> l(n + 1), r(n + 1);
l[0] = r[n] = e();
for (int i = 0; i < n; i++) {
l[i + 1] = add(l[i], k[i], a[i]);
}
for (int i = n - 1; i >= 0; i--) {
r[i] = add(r[i + 1], k[i], a[i]);
}
vector<ll> ori(n);
vector<S> init(n);
vector<vector<pair<int, ll>>> change(n);
for (int i = 0; i < n; i++) {
vector<pair<ll, ll>> cand;
for (auto p: l[i]) {
if (p.second != k[i]) cand.push_back(p);
}
assert(cand.size() >= 3);
ori[i] = a[i] + cand[0].first + cand[1].first;
init[i] = single(k[i], ori[i]);
for (int j = 0; j < 2; j++) {
if (cand[j].second < 0) continue;
ll now = ori[i] - cand[j].first + cand[2].first;
change[cand[j].second].emplace_back(i, now);
}
}
atcoder::segtree<S, op, e> st(init);
ll ans = -1;
for (int c = 0; c < n; c++) {
for (auto [i, now]: change[c]) {
st.set(i, single(k[i], now));
}
for (int i: ls[c]) {
vector<pair<ll, ll>> cand;
for (auto p: r[i + 1]) {
if (p.second != k[i]) cand.push_back(p);
}
assert(cand.size() >= 3);
S best = st.prod(0, i);
for (auto p: best) {
if (p.second == k[i]) continue;
ll now;
if (p.second == cand[0].second) {
now = cand[1].first + cand[2].first;
} else if (p.second == cand[1].second) {
now = cand[0].first + cand[2].first;
} else {
now = cand[0].first + cand[1].first;
}
now += p.first + a[i];
ans = max(ans, now);
}
}
for (auto [i, _]: change[c]) {
st.set(i, single(k[i], ori[i]));
}
}
cout << ans << endl;
}
Misuki发布的假算翻译
随机解
考虑将每种颜色均匀随机地映射到
因此,我们将不同颜色的数量减少到
最后,如果我们运行算法的次数足够多,我们就能以很高的概率找到正确答案。例如,运行
AC记录
后注
翻译:DeepL
原题解:
https://atcoder.jp/contests/abc447/editorial/16733
https://atcoder.jp/contests/abc447/editorial/16741