CF392D Three Arrays 题解
Three Arrays
推销我的博客园
题意
给定
求
数据范围
思路
一共三维,似乎不好处理,考虑先暴力枚举第一维
此时,一部分数已经在
对于每个数,可以先预处理出来要包含它的最小
详细处理每个点
不妨把它丢到坐标系上,新建一个关键点
举个例子,假如现在有如上这些点,那么此时我的两条直线可取的值应该是这些(每组直线用不同颜色区分开):
可以发现,仅有
观察发现这条折线具有单调性,想到用 set 去维护其上的那些点。
如果此时新加入了一个点
可以发现,原本折线上的点
结论出来了,对于一个新加入的点
那么答案怎么求?很简单,答案是折线上每两个相邻点
注意两种特殊情况,即一条直线压在坐标轴上,另一条直线左方或下方包含了所有点,这个也简单,初始时在 set 中加入两个点
(0,+\infty),(+\infty,0) 即可,顺便还帮忙解决了复杂的边界判断,不用担心加点求值时访问越界了。
回过头去,现在已经能动态维护
如果有细节问题可以去看代码,附带注释。
Code
#include <iostream>
#include <algorithm>
#include <set>
#define _1 (__int128)1
using namespace std;
using ll = long long;
using pii = pair<int, int>;
void FileIO (const string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int N = 3e5 + 10, INF = 1e9;
int n, a[N], b[N], c[N], all[N], m, w[N][3], ans;
set<pii> st;
multiset<int> now;
void Erase (int x, int y) {
auto it = st.lower_bound({x, y});
now.erase(now.lower_bound(prev(it)->first + it->second));
now.erase(now.lower_bound(it->first + next(it)->second));
now.insert(prev(it)->first + next(it)->second), st.erase(it);
}
void Add (int x, int y) {
st.insert({x, y});
auto it = st.lower_bound({x, y});
now.erase(now.lower_bound(prev(it)->first + next(it)->second));
now.insert(prev(it)->first + it->second);
now.insert(it->first + next(it)->second);
}
// 每次加入删除时都暴力地用类似链表的方式更改,可能常数比较大?
// 但复杂度不高,跑的还是很快的
void Insert (int x, int y) {
auto it = st.lower_bound({x, 0});
if (it != st.end() && it->second >= y) return ; // 右上方有点,无法加入折线
it = prev(st.lower_bound({x + 1, 0})); // 注意它可以踢掉 (x, y - 1) 等这种 x 相同的点
while (it->second < y) {
auto nxt = prev(it);
Erase(it->first, it->second), it = nxt;
// 一路往左,踢掉左下方的折线上的点
}
Add(x, y); // 加入折线
}
signed main () {
ios::sync_with_stdio(0), cin.tie(0);
// FileIO("");
cin >> n, ans = 3 * n, st.insert({0, INF + 10}), st.insert({INF + 10, 0}), now.insert(0); // 加入边界点
for (int i = 1; i <= n; i++)
cin >> a[i], all[++m] = a[i];
for (int i = 1; i <= n; i++)
cin >> b[i], all[++m] = b[i];
for (int i = 1; i <= n; i++)
cin >> c[i], all[++m] = c[i];
sort(all + 1, all + m + 1), m = unique(all + 1, all + m + 1) - all - 1; // 数据范围太大,先离散化
for (int i = 1; i <= m; i++) w[i][0] = w[i][1] = w[i][2] = INF; // 初始值赋 INF,表示无法在 a/b/c 中找到它
for (int i = 1; i <= n; i++)
a[i] = lower_bound(all + 1, all + m + 1, a[i]) - all, w[a[i]][0] = min(w[a[i]][0], i);
for (int i = 1; i <= n; i++)
b[i] = lower_bound(all + 1, all + m + 1, b[i]) - all, w[b[i]][1] = min(w[b[i]][1], i);
for (int i = 1; i <= n; i++)
c[i] = lower_bound(all + 1, all + m + 1, c[i]) - all, w[c[i]][2] = min(w[c[i]][2], i);
for (int i = 1; i <= m; i++)
if (w[i][0] == INF)
Insert(w[i][1], w[i][2]); // 对于本身不在 a 中的点,先将其加入坐标系
if (now.size()) ans = min(ans, n + *now.begin());
for (int i = n; i; i--) {
if (w[a[i]][0] == i) // 只有当它无法被 a[1...u] 包含时才将其加入
Insert(w[a[i]][1], w[a[i]][2]);
if (now.size()) ans = min(ans, i - 1 + *now.begin());
// 倒着枚举,动态维护折线和答案
}
cout << ans;
return 0;
}