跳到正文

题解

题解:AT_awtf2025_c Get Closer

_Z_F_R_约 11 分钟
原文
文章信息与版本

文章信息

收录
2026年3月6日
更新
2026年3月6日
篇幅
约 11 分钟

版本

  1. v2当前
  2. v1

尝试二分答案,对于判定问题我们形式化地描述如下:

  • 给定 t,{ai}t, \{a_i\},判断是否存在 {bi}\{b_i\} 满足:
    • 0≤bi≤ai0 \le b_i \le a_i;
    • ∀i∈[1,n]\forall i \in [1, n],−bi+∑1≤j<iaj−2bj≥0-b_i + \sum_{1 \le j < i} a_j - 2b_j \ge 0,其中 n=∣{ai}∣n = |\{a_i\}|;
    • ∀i∈[1,n)\forall i \in [1, n),ai−bi+bi+1≤ta_i - b_i + b_{i + 1} \le t。

设计 dp,考虑 fx,yf_{x, y} 表示,目前考虑到长度为 ii 的前缀,当前 ∑i≤xai−2bi=y\sum_{i \le x} a_i - 2b_i = y(也即,需要向右匹配的点的数量),最小的 ax−bxa_x - b_x 是多少,可以做到 O((∑ai)2log⁡(max⁡ai))O((\sum a_i)^2 \log (\max a_i)),太慢了。

对于一个 xx,把所有合法的 (fx,y,y)(f_{x, y}, y) 画在坐标系上。

我们每次 x←x+1x \gets x + 1 时,会:

  • 保留满足以下条件的 (p,q)(p, q):存在 r∈[0,ax+1]r \in [0, a_{x + 1}] 满足:q−r≥0q - r \ge 0,及 p+r≤tp + r \le t。
  • 然后加入 (ax+1−r,q+ax+1−2r)(a_{x + 1} - r, q + a_{x + 1} - 2r)。
  • 最后对于每个 qq,只保留最小的 pp,满足 (p,q)(p, q) 存在于集合中。

特别的:

  • 我们初始会加入 (0,0)(0, 0)。
  • 我们最后只关心集合中是否有 (0,0)(0, 0)。

这个条件十分的烦,但是刚才的 dp 中,我们已经利用了如下性质:对于 (p,q)(p, q) 和 (p′,q)(p', q) 两个点,如果 p<p′p < p',那么 (p′,q)(p', q) 严格不优于 (p,q)(p, q)。

对于 (p,q)(p, q),我们加入所有的 (p′,q)(p', q),那么 p+r≤tp + r \le t 可以变成 p+r=tp + r = t,于是条件变为:t−p∈[0,ax+1]t - p \in [0, a_{x + 1}],且 q−(t−p)≥0q - (t - p) \ge 0。我们会加入 (ax+1−t+p,q+ax+1−2t+2p)(a_{x + 1} - t + p, q + a_{x + 1} - 2t + 2p)。

条件化简为:t−ax+1≤p≤tt - a_{x + 1} \le p \le t,p+q≥tp + q \ge t。

可以归纳证明:

  • 按照上述转移,任意时刻对于一个 pp,合法的 qq 是一个区间;

  • 对于每个区间,qq 的下界相同。qq 的上界可视为一个分段一次函数。

  • 进一步的,qq 的上界关于 pp 的函数 g(p)g(p) 是上凸的。

    原因是:

    • g(p)g(p) 有 g(p+1)≥g(p)g(p + 1) \ge g(p) 的性质。
    • 对 g(p)g(p) 进行如下两种操作均不改变凸性:
      • 所有 g(p)←g(p)+kp+bg(p) \gets g(p) + kp + b,其中 k≥0k \ge 0;
      • 将 p≥p0p \ge p_0 的部分的 g(p)g(p) 设为 g(p0)g(p_0)。

我们维护 g(p)g(p) 的断点,然后用双端队列增删断点,打全局 tag 来支持平移和 g(p)←g(p)+kx+bg(p) \gets g(p) + kx + b 的操作,那么单次判定就是 O(n)O(n) 的了,总复杂度就是 O(nlog⁡(max⁡ai))O(n \log (\max a_i))。

C++
// Think TWICE, code ONCE
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline ll Read() {
    int sig = 1; ll num = 0; char c = getchar();
    while(!isdigit(c)) { if(c == '-') sig = -1; c = getchar(); }
    while(isdigit(c)) num = (num << 3) + (num << 1) + (c ^ 48), c = getchar();
    return num * sig;
}
void Write(ll x) {
    if(x < 0) putchar('-'), x = -x;
    if(x > 9) Write(x / 10);
    putchar((x % 10) ^ 48);
}
const int N = 250005;
struct Figure {
    int front, back; ll x, y, dtx, dtk, dtb;
    pair<ll, ll> q[N << 1];
    void Init() {
        x = y = dtx = dtk = dtb = 0, front = N, back = N - 1;
        q[++back] = make_pair(0ll, 0ll);
    }
    inline ll GetX(pair<ll, ll> p) { return p.first + dtx;  }
    inline ll GetY(pair<ll, ll> p) { return p.second + dtk * p.first + dtb; }
    inline pair<ll, ll> Encode(pair<ll, ll> p) {
        p.first -= dtx, p.second -= dtk * p.first + dtb;
        return p;
    }
    void Restrict(ll l, ll r) {
        pair<ll, ll> lst = make_pair(-1ll, -1ll);
        while(front <= back && GetX(q[front]) < l) lst = q[front++];
        while(front <= back && r - GetX(q[front]) > GetY(q[front])) lst = q[front++];
        if(lst.first != -1) {
            if(front <= back) {
                ll k = (GetY(q[front]) - GetY(lst)) / (q[front].first - lst.first);
                if(k * (l - GetX(lst)) + GetY(lst) >= r - l) {
                    if(GetX(q[front]) > l) q[--front] = Encode(make_pair(l, k * (l - GetX(lst)) + GetY(lst)));
                    pair<ll, ll> p = make_pair(l, max(GetY(make_pair(x, y)), r - l));
                    p = Encode(p), x = p.first, y = p.second;
                }
                else {
                    pair<ll, ll> p;
                    p.first = (k * GetX(lst) - GetY(lst) + k + r) / (k + 1);
                    p.second = k * (p.first - GetX(lst)) + GetY(lst);
                    if(GetX(q[front]) > p.first) q[--front] = Encode(p);
                    p.second = r - p.first, p = Encode(p), x = p.first, y = p.second;
                }
            }
            else {
                if(GetY(lst) >= r - l) {
                    q[--front] = Encode(make_pair(l, GetY(lst)));
                    pair<ll, ll> p(l, max(GetY(make_pair(x, y)), r - l));
                    p = Encode(p), x = p.first, y = p.second;
                }
                else {
                    pair<ll, ll> p;
                    p.second = GetY(lst), p.first = r - p.second, p = Encode(p);
                    q[--front] = p, x = p.first, y = p.second;
                }
            }
        }
        else if(GetY(make_pair(x, y)) < r - GetX(make_pair(x, y))) {
            pair<ll, ll> p; p.first = GetX(make_pair(x, y)), p.second = r - p.first;
            p = Encode(p), x = p.first, y = p.second;
        }
        lst = make_pair(-1ll, -1ll);
        while(front <= back && GetX(q[back]) > r) lst = q[back--];
        if(front > back) return ;
        if(GetX(q[back]) < r) {
            if(lst.first != -1) {
                ll k = (GetY(lst) - GetY(q[back])) / (lst.first - q[back].first);
                pair<ll, ll> p(r, k * (r - GetX(q[back])) + GetY(q[back]));
                q[++back] = Encode(p), lst = make_pair(-1ll, -1ll);
            }
            else { pair<ll, ll> p = Encode(make_pair(r, GetY(q[back]))); q[++back] = p; }
        }
    }
    void Move(ll _dtx, ll _dty) { dtx += _dtx, dtb += _dty; }
    void Add2() { dtk += 2, dtb += 2ll * dtx; }
}ds;
int n;
ll a[N];
bool Check(ll t) {
    int i; ds.Init();
    for(i = 1; i <= n; i++) ds.Restrict(t - a[i], t), ds.Add2(), ds.Move(a[i] - t, a[i] - 2ll * t);
    return ds.x + ds.dtx == 0 && ds.y + ds.dtk * ds.x + ds.dtb == 0;
}
void Solve() {
    int i; n = Read();
    for(i = 1; i <= n; i++) a[i] = Read();
    ll l = 0, r = 2e9, mid, res = r;
    while(l <= r) {
        mid = (l + r) >> 1;
        if(Check(mid)) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    Write(res), putchar('\n');
}
int main() {
    int T = Read();
    while(T--) Solve();
}
原文