题解:CF1884E Hard Design
stripe_python · · 题解
独立想出 *2800 纪念。想到做法花了 1h,写代码用了 2h,得练码力了(
不难发现所有数字都变成最大值时,操作次数最小。
设最大值为
根据该问题的结论,第一问的答案即为
该值可以在循环位移过程中方便地维护。
对于第二问,我们从另一种视角来看待积木大赛:
我们定义一个插头为一条可以向左右延伸的操作区间。
假设已处理完左侧,当前位于位置
i ,左侧传来的右插头数量为k ,则:
- 若
k \ge a_i ,则全部使用这些插头,并生成a_i 个新的右插头;- 若
k < a_i ,则使用全部插头后还需额外进行a_i-k 次操作,并生成a_i 个新的右插头。
记这
- 若
k \ge a_i :所有插头均可在此处被使用。每个插头贡献的长度为i-p_x+1 ,因此代价为
处理完毕后,
-
若
k < a_i :此时需要额外创建a_i-k 个插头,这些插头的起点就是i 。我们还需决定k 个已有插头中哪些与当前的a_i 配对。根据直觉,越靠前的插头优先配对,得到的代价越小。
考虑证明这一观察。不妨考虑二元情况,即两个插头配对两个点的情况。
设两个插头的起点为
a,b ,当前处理位置为c ,之后存在另一配对点d 。则a<b<c<d 。要证(b-c)^2+(a-d)^2>(a-c)^2+(b-d)^2 ,即证\begin{aligned} (b-c)^2+(a-d)^2-(a-c)^2-(b-d)^2 &> 0\\ bc+ad-ac-bd &> 0\\ a(d-c)+b(c-d) &> 0\\ (b-a)(d-c) &> 0\\ \end{aligned} 上式显然成立。
因此,应将最靠前的
处理完毕后,
综上所述,我们需要一种数据结构维护插头起点集合
进一步,由于转化后
从断点向左和向右做两次扫描,处理出序列的前缀代价和后缀代价。将结果合并,即可得到完整答案。
#define int long long
const int N = 1e6 + 5;
mt19937 rand32(chrono::steady_clock::now().time_since_epoch().count());
struct node {
unsigned prio;
node *ls, *rs;
int start, cnt, sum;
mint x, x2;
node(int a = 0, int b = 0)
: prio(rand32()), ls(nullptr), rs(nullptr), start(a),
cnt(b), sum(b), x((mint)a * b), x2((mint)a * a * b) {}
node* pushup() {
sum = cnt, x = (mint)start * cnt, x2 = (mint)start * start * cnt;
if (ls) sum += ls->sum, x += ls->x, x2 += ls->x2;
if (rs) sum += rs->sum, x += rs->x, x2 += rs->x2;
return this;
}
mint calc(mint pos) {return pos * pos * sum - x * pos * 2 + x2;}
};
node* merge(node* u, node* v) {
if (!u || !v) return u ? u : v;
return (u->prio < v->prio) ?
(u->rs = merge(u->rs, v), u->pushup()) :
(v->ls = merge(u, v->ls), v->pushup());
}
void split(node* u, int k, node*& x, node*& y) {
if (!u) return x = y = nullptr, void();
int ls = u->ls ? u->ls->sum : 0;
if (k <= ls) {
y = u, split(u->ls, k, x, u->ls), u->pushup();
} else if (k < ls + u->cnt) {
y = new node(u->start, u->cnt - k + ls), y->rs = u->rs, y->pushup();
u->cnt = k - ls, x = u, u->rs = nullptr, u->pushup();
} else {
x = u, split(u->rs, k - ls - u->cnt, u->rs, y), u->pushup();
}
}
int n, a[N];
pair<int, mint> pre[N], suf[N];
void solve(int n, pair<int, mint>* ans) {
node *rt = nullptr;
int cnt = 0; mint cost = 0;
for (int i = 1; i < n; i++) {
cnt += max(0LL, a[i] - a[i - 1]);
int ln = rt ? rt->sum : 0;
if (a[i] > ln) {
rt = merge(rt, new node(i, a[i] - ln));
} else if (a[i] < ln) {
node *x, *y; split(rt, a[i], x, y);
cost += y ? y->calc(i) : 0, rt = x;
}
ans[i] = {cnt, cost + (rt ? rt->calc(i + 1) : 0)};
}
}
void _main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> a[i];
if (n == 1) return cout << "0 0\n", void();
int rot = max_element(a, a + n) - a, m = a[rot];
rotate(a, a + rot, a + n);
for (int i = 0; i < n; i++) a[i] = m - a[i];
solve(n, pre);
reverse(a + 1, a + n), solve(n, suf), reverse(suf + 1, suf + n);
for (int i = 0; i < n; i++) {
int k = (i - rot + n) % n, cnt = 0; mint cost = 0;
if (k > 0) cnt += suf[k].first, cost += suf[k].second;
if (k > 1) cnt += pre[k - 1].first, cost += pre[k - 1].second;
if (k == 0) cnt += pre[n - 1].first, cost += pre[n - 1].second;
cout << cnt << ' ' << cost << '\n';
}
}