题解:AT_awtf2025_c Get Closer
尝试二分答案,对于判定问题我们形式化地描述如下:
- 给定
t, \{a_i\} ,判断是否存在\{b_i\} 满足: -
设计 dp,考虑
对于一个
我们每次
- 保留满足以下条件的
(p, q) :存在r \in [0, a_{x + 1}] 满足:q - r \ge 0 ,及p + r \le t 。 - 然后加入
(a_{x + 1} - r, q + a_{x + 1} - 2r) 。 - 最后对于每个
q ,只保留最小的p ,满足(p, q) 存在于集合中。
特别的:
- 我们初始会加入
(0, 0) 。 - 我们最后只关心集合中是否有
(0, 0) 。
这个条件十分的烦,但是刚才的 dp 中,我们已经利用了如下性质:对于
对于
条件化简为:
可以归纳证明:
-
按照上述转移,任意时刻对于一个
p ,合法的q 是一个区间; -
对于每个区间,
q 的下界相同。q 的上界可视为一个分段一次函数。 -
进一步的,
q 的上界关于p 的函数g(p) 是上凸的。原因是:
-
- 对
g(p) 进行如下两种操作均不改变凸性:- 所有
g(p) \gets g(p) + kp + b ,其中k \ge 0 ; - 将
p \ge p_0 的部分的g(p) 设为g(p_0) 。
- 所有
-
我们维护
// 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();
}