题面
来源:USACO 官网。模型收到的是同一份题面的文字。
Bessie is experimenting with a powerful hoof implant that has the ability to create massive shock waves. She has $N$ ($2 \leq N \leq 10^5$) tiles lined up in front of her, which require powers of at least $p_0,p_1,\dots,p_{N-1}$ to break, respectively ($0 \leq p_i \leq 10^{18}$).
Bessie can apply power by punching a specific tile but due to the strange nature of her implant, it will not apply any power to the tile she punches. Instead, if she chooses to punch tile $x$ once, where $x$ is an integer in $[0,N-1]$, it applies $|i-x|$ power to tile $i$ for all integers $i$ in the range $[0,N-1]$. This power is also cumulative, so applying $2$ power twice to a tile will apply a total of $4$ power to the tile.
Please determine the fewest number of punches required to break all the tiles.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains $T$ ($1 \leq T \leq 100$) representing the number of test cases.Line $2t$ contains a single integer $N$, the number of tiles in test case $t$.
Line $2t+1$ contains $N$ space separated numbers $p_0,p_1, \ldots, p_{N-1}$ representing that tile $i$ takes $p_i$ power to be broken.
It is guaranteed that the sum of all $N$ in a single input does not exceed $5\cdot 10^5$.
OUTPUT FORMAT (print output to the terminal / stdout):
$T$ lines, the $i$th line representing the answer to the $i$th test case.SAMPLE INPUT:
6 5 0 2 4 5 8 5 6 5 4 5 6 5 1 1 1 1 1 5 12 10 8 6 4 7 6 1 2 3 5 8 13 2 1000000000000000000 1000000000000000000
SAMPLE OUTPUT:
2 3 2 4 4 2000000000000000000
For the first test, the only way for Bessie to break all the tiles with two punches is to punch $0$ twice, applying total powers of $[0,2,4,6,8]$ respectively.
For the second test, one way for Bessie to break all the tiles with three punches is to punch $0$, $2$, and $4$ one time each, applying total powers of $[6,5,4,5,6]$ respectively.
For the third test, one way for Bessie to break all the tiles with two punches is to punch $0$ and $1$ one time each, applying total powers of $[1,1,3,5,7]$ respectively.
SCORING:
- Input 2: All $p_i$ are equal.
- Inputs 3-6: $N\le 100$
- Inputs 7-14: No additional constraints.
Problem credits: Suhas Nagar
结果
Kimi K3.1 ProKimi
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 49.0k平均输出 token(含思考) ↓
- 24.8输出速度 tokens/s ↑
- 2036 秒延迟 ↓
- 0.71×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
Kimi K3.1 Pro(high)Kimi high
1/3
解出的采样 ↑
- 14.3采样 1 · 答案错误
- 100采样 2 · 解出
- 14.3采样 3 · 答案错误
- 34.4k平均输出 token(含思考) ↓
- 21.5输出速度 tokens/s ↑
- 1549 秒延迟 ↓
- 1.24×CPU 时间 / 官方解 ↓
- 同阶 1时间复杂度(相对官方解)
DeepSeek Flash V4.1-1017DeepSeek
1/3
解出的采样 ↑
- 100采样 1 · 解出
- 14.3采样 2 · 答案错误
- 7.1采样 3 · 答案错误
- 123.0k平均输出 token(含思考) ↓
- 185.1输出速度 tokens/s ↑
- 660 秒延迟 ↓
- 9.33×CPU 时间 / 官方解 ↓
- 同阶 1时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
1/3
解出的采样 ↑
- 0.0采样 1 · 编译错误
- 100采样 2 · 解出
- 7.1采样 3 · 答案错误
- 87.0k平均输出 token(含思考) ↓
- 111.9输出速度 tokens/s ↑
- 733 秒延迟 ↓
- 3.82×CPU 时间 / 官方解 ↓
- 同阶 1时间复杂度(相对官方解)
↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct Rat { i128 n; int64 d; }; struct Interval { bool ok; Rat lo, hi; }; // Interval of real a for which a punches at 0 and K-a punches at N-1 // (fractional punches allowed) cover all requirements. Interval getInterval(int64 K, const vector<int64>& p) { const int N = (int)p.size(); const int L = N - 1; Interval res{true, {0, 1}, {(i128)K, 1}}; for (int i = 0; i < N; ++i) { i128 base = (i128)K * (L - i); int64 d = 2LL * i - L; if (d == 0) { if (base < (i128)p[i]) { res.ok = false; return res; } } else if (d > 0) { i128 num = (i128)p[i] - base; if (num > 0 && num * res.lo.d > res.lo.n * d) { res.lo = {num, d}; } } else { int64 e = -d; i128 num = base - (i128)p[i]; if (num < 0) { res.ok = false; return res; } if (num * res.hi.d < res.hi.n * e) { res.hi = {num, e}; } } } res.ok = (res.lo.n * res.hi.d <= res.hi.n * res.lo.d); return res; } // Exact feasibility with K integral punches. bool integerFeasible(int64 K, const vector<int64>& p) { const int N = (int)p.size(); const int L = N - 1; Interval in = getInterval(K, p); if (!in.ok) return false; i128 ceilLo = in.lo.n / in.lo.d; if (in.lo.n % in.lo.d != 0) ++ceilLo; i128 floorHi = in.hi.n / in.hi.d; // Integral endpoint-only solution. if (ceilLo <= floorHi) return true; // Otherwise only one possible pair of endpoint base counts remains. i128 q = in.lo.n / in.lo.d; if (q < 0 || q >= (i128)K) return false; int64 A = (int64)q; int64 B = K - 1 - A; // Check A punches at 0, B punches at N-1, and one additional punch x. vector<i128> r(N), suffix(N); for (int i = 0; i < N; ++i) { i128 base = (i128)A * i + (i128)B * (L - i); r[i] = (i128)p[i] - base; } const i128 INF = i128(1) << 120; i128 mn = INF; for (int i = N - 1; i >= 0; --i) { mn = min(mn, (i128)i - r[i]); suffix[i] = mn; } i128 mx = -INF; for (int x = 0; x < N; ++x) { mx = max(mx, (i128)x + r[x]); if (mx <= (i128)x && (i128)x <= suffix[x]) return true; } return false; } // Ceiling of the fractional (linear programming) optimum. int64 fractionalCeil(const vector<int64>& p) { const int N = (int)p.size(); const int L = N - 1; vector<int> hull; hull.reserve(N); // Upper concave hull of (i,p_i). for (int i = 0; i < N; ++i) { while ((int)hull.size() >= 2) { int a = hull[(int)hull.size() - 2]; int b = hull.back(); i128 lhs = ((i128)p[b] - p[a]) * (i - b); i128 rhs = ((i128)p[i] - p[b]) * (b - a); if (lhs <= rhs) hull.pop_back(); else break; } hull.push_back(i); } // Evaluate the hull at x=L/2. int e = 0; while (e + 1 < (int)hull.size() && 2LL * hull[e + 1] < L) ++e; int x1 = hull[e], x2 = hull[e + 1]; int64 dx = x2 - x1; i128 den = (i128)L * dx; i128 num = 2 * (i128)p[x1] * dx + ((i128)p[x2] - p[x1]) * (L - 2LL * x1); if (num <= 0) return 0; return (int64)((num + den - 1) / den); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); for (auto &x : p) cin >> x; int64 m = fractionalCeil(p); int64 answer = integerFeasible(m, p) ? m : m + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; using int64 = long long; struct Fraction { i128 num; int64 den; // always positive }; static int cmpFraction(const Fraction &a, const Fraction &b) { i128 left = a.num * b.den; i128 right = b.num * a.den; if (left < right) return -1; if (left > right) return 1; return 0; } static i128 floorDiv(i128 a, int64 b) { if (a >= 0) return a / b; return -((-a + b - 1) / b); } static i128 ceilDiv(i128 a, int64 b) { if (a >= 0) return (a + b - 1) / b; return -((-a) / b); } struct TestSolver { int n; int64 L; vector<int64> p; bool getRealInterval(int64 m, Fraction &lower, Fraction &upper) const { lower = {0, 1}; upper = {(i128)L * m, 1}; for (int i = 0; i < n; ++i) { i128 mi = (i128)m * i; int64 d = L - 2LL * i; if (d == 0) { if ((i128)p[i] > mi) return false; continue; } i128 num = (i128)L * ((i128)p[i] - mi); Fraction f{num, d}; if (f.den < 0) { f.den = -f.den; f.num = -f.num; } if (d > 0) { if (cmpFraction(f, lower) > 0) lower = f; } else { if (cmpFraction(f, upper) < 0) upper = f; } if (cmpFraction(lower, upper) > 0) return false; } return true; } bool feasible(int64 m) const { Fraction lower, upper; if (!getRealInterval(m, lower, upper)) return false; i128 aLo = ceilDiv(lower.num, lower.den); i128 aHi = floorDiv(upper.num, upper.den); if (aLo > aHi) return false; // If A is a multiple of L, punches only at the two endpoints suffice. i128 firstMultiple = ceilDiv(aLo, L) * L; if (firstMultiple <= aHi) return true; // Otherwise all possible A values have the same quotient b=A/L. i128 b = aLo / L; int64 rLo = (int64)(aLo % L); int64 rHi = (int64)(aHi % L); if (rLo > rHi || rLo <= 0 || rHi >= L) return false; // b punches at L, m-b-1 punches at 0, and one punch at r. // At tile i the power is base_i + |i-r|. vector<int> diff(L + 2, 0); for (int i = 0; i < n; ++i) { i128 base = (i128)(m - 1) * i + b * (L - 2LL * i); i128 need = (i128)p[i] - base; if (need <= 0) continue; // Every available r has |i-r| <= L-1. if (need >= L) return false; int64 t = (int64)need; int64 forbiddenL = max(rLo, (int64)i - t + 1); int64 forbiddenR = min(rHi, (int64)i + t - 1); if (forbiddenL <= forbiddenR) { ++diff[forbiddenL]; --diff[forbiddenR + 1]; } } int covered = 0; for (int64 r = rLo; r <= rHi; ++r) { covered += diff[r]; if (covered == 0) return true; } return false; } int64 solve() const { int64 lo = 0, hi = 4000000000000000000LL; while (lo < hi) { int64 mid = lo + (hi - lo) / 2; if (feasible(mid)) hi = mid; else lo = mid + 1; } return lo; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<int64> p(n); for (auto &x : p) cin >> x; if (n == 2) { cout << p[0] + p[1] << '\n'; continue; } TestSolver solver{n, n - 1, std::move(p)}; cout << solver.solve() << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128_t; struct Rat { i128 n; int d; // always positive }; struct Bounds { bool ok; Rat lo, hi; }; // For a fixed K, decide whether fractional punches at the two endpoints // can cover all tiles. beta is the amount punched at tile L=N-1, and // K-beta is punched at tile 0. static Bounds getBounds(ll K, const vector<ll>& p, int L) { i128 k = (i128)K; Rat lo{0, 1}, hi{(i128)K, 1}; bool ok = true; for (int i = 0; i <= L; ++i) { // K*i + beta*(L-2*i) >= p[i] i128 num = (i128)p[i] - k * (i128)i; int c = L - 2 * i; if (c == 0) { if (num > 0) { ok = false; break; } } else if (c > 0) { // beta >= num/c if (num * (i128)lo.d > lo.n * (i128)c) { lo = {num, c}; } } else { int d = -c; // beta <= (K*i-p[i])/d i128 upperNum = -num; if (upperNum * (i128)hi.d < hi.n * (i128)d) { hi = {upperNum, d}; } } } if (ok && lo.n * (i128)hi.d > hi.n * (i128)lo.d) { ok = false; } return {ok, lo, hi}; } static i128 floorDiv(i128 n, i128 d) { i128 q = n / d, r = n % d; if (r < 0) --q; return q; } static i128 ceilDiv(i128 n, i128 d) { i128 q = n / d, r = n % d; if (r > 0) ++q; return q; } // Check whether K integral punches suffice. static bool integralFeasible(ll K, const vector<ll>& p, int L) { Bounds bounds = getBounds(K, p, L); if (!bounds.ok) return false; // An integral beta in this interval gives an endpoint-only solution. i128 smallestInt = ceilDiv(bounds.lo.n, bounds.lo.d); i128 largestInt = floorDiv(bounds.hi.n, bounds.hi.d); if (smallestInt <= largestInt) return true; // Otherwise every feasible real beta lies strictly between b and b+1. // After spreading punches apart, an integral solution can be assumed // to have b punches at L, K-1-b punches at 0, and one punch at r. i128 b = floorDiv(bounds.lo.n, bounds.lo.d); if (b < 0 || b > (i128)(K - 1)) return false; const int N = L + 1; const i128 INF = ((i128)1 << 120); i128 k = (i128)K; vector<i128> R(N), A(N), B(N); for (int i = 0; i < N; ++i) { i128 c = (i128)(L - 2 * i); i128 base = (k - 1) * (i128)i + b * c; R[i] = (i128)p[i] - base; // For the remaining punch at r: // |i-r| >= R[i]. A[i] = (i128)i + R[i]; // r >= A[i] when i < r B[i] = (i128)i - R[i]; // r <= B[i] when i > r } vector<i128> prefixMax(N), suffixMin(N); prefixMax[0] = -INF; for (int r = 1; r < N; ++r) { prefixMax[r] = max(prefixMax[r - 1], A[r - 1]); } suffixMin[N - 1] = INF; for (int r = N - 2; r >= 0; --r) { suffixMin[r] = min(suffixMin[r + 1], B[r + 1]); } for (int r = 0; r < N; ++r) { if (R[r] <= 0 && prefixMax[r] <= (i128)r && suffixMin[r] >= (i128)r) { return true; } } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<ll> p(N); ll pmax = 0; for (ll& x : p) { cin >> x; pmax = max(pmax, x); } if (pmax == 0) { cout << 0 << '\n'; continue; } int L = N - 1; // Two equal integral endpoint allocations give an always-feasible // upper bound: C*L >= pmax at every tile. ll C = (pmax + L - 1) / L; ll lo = 0, hi = 2 * C; // M is the smallest integer K feasible for the LP relaxation. while (lo < hi) { ll mid = lo + (hi - lo) / 2; if (getBounds(mid, p, L).ok) { hi = mid; } else { lo = mid + 1; } } ll M = lo; // The integral optimum is always either M or M+1. ll answer = integralFeasible(M, p, L) ? M : M + 1; cout << answer << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; using int64 = long long; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); int64 maxP = 0; for (int i = 0; i < N; i++) { cin >> p[i]; maxP = max(maxP, p[i]); } const int L = N - 1; vector<int> diff(N + 2); auto feasible = [&](int64 k) -> bool { if (k == 0) return maxP == 0; const i128 K = (i128)k * L; i128 slo = 0, shi = K; // If S = q[0], then q[N-1] = k*L-S. Since q is convex, // q[i] is at most the linear interpolation of its endpoints. for (int i = 0; i < N; i++) { const int64 c = L - 2LL * i; const i128 R = (i128)L * (p[i] - (i128)i * k); if (c > 0) { if (R > 0) { i128 lower = (R + c - 1) / c; slo = max(slo, lower); } } else if (c < 0) { if (R > 0) return false; i128 upper = (-R) / (-c); shi = min(shi, upper); } else { if (R > 0) return false; } if (slo > shi || shi < 0 || slo > K) return false; } // Punches only at the two endpoints. Such a configuration exists // exactly when an integer multiple of L lies in [slo, shi]. i128 v = (slo + L - 1) / L; if (v <= k && v * L <= shi) return true; if (L == 1) return false; // Otherwise [slo,shi] has length < L, so only O(1) values of v // need to be considered. A maximally spread configuration has // v punches at L, one punch at x, and all remaining punches at 0. i128 vs = slo / L - 1; i128 ve = shi / L + 1; int64 vStart = vs > 0 ? (int64)vs : 0; int64 vEnd = ve < (i128)(k - 1) ? (int64)ve : k - 1; vStart = max<int64>(vStart, 0); for (int64 cntRight = vStart; cntRight <= vEnd; cntRight++) { i128 baseS = (i128)cntRight * L; i128 xl = slo - baseS; i128 xr = shi - baseS; xl = max(xl, (i128)1); xr = min(xr, (i128)(L - 1)); if (xl > xr) continue; int xlo = (int)xl; int xhi = (int)xr; fill(diff.begin(), diff.begin() + L + 2, 0); for (int i = 0; i < N; i++) { i128 basePower = (i128)(k - cntRight - 1) * (L - i) + (i128)cntRight * i; i128 d = (i128)p[i] - basePower; if (d <= 0) continue; // We need |i-x| >= d. Thus the following interval of x // values is forbidden. i128 left = (i128)i - d + 1; i128 right = (i128)i + d - 1; left = max(left, (i128)xlo); right = min(right, (i128)xhi); if (left <= right) { diff[(int)left]++; diff[(int)right + 1]--; } } int active = 0; for (int x = xlo; x <= xhi; x++) { active += diff[x]; if (active == 0) return true; } } return false; }; int64 low = 0, high = 2 * maxP; while (low < high) { int64 mid = low + (high - low) / 2; if (feasible(mid)) high = mid; else low = mid + 1; } cout << low << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; using int64 = long long; struct Fraction { i128 num; int64 den; // always positive }; static int cmpFraction(const Fraction &a, const Fraction &b) { i128 left = a.num * b.den; i128 right = b.num * a.den; if (left < right) return -1; if (left > right) return 1; return 0; } static i128 floorDiv(i128 a, int64 b) { if (a >= 0) return a / b; return -((-a + b - 1) / b); } static i128 ceilDiv(i128 a, int64 b) { if (a >= 0) return (a + b - 1) / b; return -((-a) / b); } struct TestSolver { int n; int64 L; vector<int64> p; bool getRealInterval(int64 m, Fraction &lower, Fraction &upper) const { lower = {0, 1}; upper = {(i128)L * m, 1}; for (int i = 0; i < n; ++i) { i128 mi = (i128)m * i; int64 d = L - 2LL * i; if (d == 0) { if ((i128)p[i] > mi) return false; continue; } i128 num = (i128)L * ((i128)p[i] - mi); Fraction f{num, d}; if (f.den < 0) { f.den = -f.den; f.num = -f.num; } if (d > 0) { if (cmpFraction(f, lower) > 0) lower = f; } else { if (cmpFraction(f, upper) < 0) upper = f; } if (cmpFraction(lower, upper) > 0) return false; } return true; } bool feasible(int64 m) const { Fraction lower, upper; if (!getRealInterval(m, lower, upper)) return false; i128 aLo = ceilDiv(lower.num, lower.den); i128 aHi = floorDiv(upper.num, upper.den); if (aLo > aHi) return false; // If A is a multiple of L, punches only at the two endpoints suffice. i128 firstMultiple = ceilDiv(aLo, L) * L; if (firstMultiple <= aHi) return true; // Otherwise all possible A values have the same quotient b=A/L. i128 b = aLo / L; int64 rLo = (int64)(aLo % L); int64 rHi = (int64)(aHi % L); if (rLo > rHi || rLo <= 0 || rHi >= L) return false; // b punches at L, m-b-1 punches at 0, and one punch at r. // At tile i the power is base_i + |i-r|. vector<int> diff(L + 2, 0); for (int i = 0; i < n; ++i) { i128 base = (i128)(m - 1) * i + b * (L - 2LL * i); i128 need = (i128)p[i] - base; if (need <= 0) continue; // Every available r has |i-r| <= L-1. if (need >= L) return false; int64 t = (int64)need; int64 forbiddenL = max(rLo, (int64)i - t + 1); int64 forbiddenR = min(rHi, (int64)i + t - 1); if (forbiddenL <= forbiddenR) { ++diff[forbiddenL]; --diff[forbiddenR + 1]; } } int covered = 0; for (int64 r = rLo; r <= rHi; ++r) { covered += diff[r]; if (covered == 0) return true; } return false; } int64 solve() const { int64 lo = 0, hi = 4000000000000000000LL; while (lo < hi) { int64 mid = lo + (hi - lo) / 2; if (feasible(mid)) hi = mid; else lo = mid + 1; } return lo; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<int64> p(n); for (auto &x : p) cin >> x; if (n == 2) { cout << p[0] + p[1] << '\n'; continue; } TestSolver solver{n, n - 1, std::move(p)}; cout << solver.solve() << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; struct Point { int x; int64 y; }; struct Rational { i128 num = 0; i128 den = 1; }; static i128 cross(const Point &a, const Point &b, const Point &c) { return (i128)(b.x - a.x) * (c.y - a.y) - (i128)(b.y - a.y) * (c.x - a.x); } // Continuous optimum for the given required powers. static Rational continuousOptimum(const vector<int64> &p) { int n = (int)p.size(); int L = n - 1; Rational best; auto relax = [&](i128 num, i128 den) { if (num * best.den > best.num * den) { best = {num, den}; } }; // Dual solutions supported on one tile. for (int i = 0; i < n; ++i) { int d = max(i, L - i); relax((i128)p[i], (i128)d); } // Upper concave hull of (i,p_i). vector<Point> hull; hull.reserve(n); for (int i = 0; i < n; ++i) { Point cur{i, p[i]}; while (hull.size() >= 2 && cross(hull[hull.size() - 2], hull.back(), cur) >= 0) { hull.pop_back(); } hull.push_back(cur); } // Evaluate the hull at x=L/2. int pos = 0; while (pos + 1 < (int)hull.size() && 2 * hull[pos + 1].x < L) { ++pos; } Point a = hull[pos]; Point b = hull[pos + 1]; i128 num = (i128)a.y * (2 * b.x - L) + (i128)b.y * (L - 2 * a.x); i128 den = (i128)L * (b.x - a.x); relax(num, den); return best; } static void printInt128(i128 x) { if (x == 0) { cout << '0'; return; } string s; while (x > 0) { s.push_back(char('0' + (int)(x % 10))); x /= 10; } reverse(s.begin(), s.end()); cout << s; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<int64> p(n); for (auto &x : p) cin >> x; i128 answer = -1; vector<int64> adjusted(n); // K is the number of punches and a is the parity of the power at tile 0. // Every realizable sequence satisfies // f_i == a + i*K (mod 2). for (int kParity = 0; kParity <= 1; ++kParity) { for (int a = 0; a <= 1; ++a) { for (int i = 0; i < n; ++i) { int wanted = (a + kParity * (i & 1)) & 1; adjusted[i] = p[i] + (((int)(p[i] & 1) != wanted) ? 1 : 0); } Rational r = continuousOptimum(adjusted); i128 k = (r.num + r.den - 1) / r.den; if ((int)(k & 1) != kParity) ++k; if (answer == -1 || k < answer) answer = k; } } printInt128(answer); cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; const int MAXN = 100005; vector<long long> P; int bestF[MAXN], bestU2[MAXN]; vector<int> stF, hullU2, hullP, stU1; i128 floor_div(i128 a, i128 b) { // b > 0 if (a >= 0) return a / b; return - ((-a + b - 1) / b); } i128 ceil_div(i128 a, i128 b) { // b > 0 if (a >= 0) return (a + b - 1) / b; return - ((-a) / b); } bool feasible(int N, long long K) { int m = N - 1; int low_end = (m - 1) / 2; // i with 2*i < m int high_start = m / 2 + 1; // i with 2*i > m int mid_even = (m % 2 == 0) ? m / 2 : -1; stF.clear(); hullU2.clear(); int headU2 = 0; auto getA = [&](int i) -> i128 { return (i128)P[i] - (i128)(m - i) * K - 2 * i + m; }; auto getC = [&](int i) -> i128 { return (i128)P[i] - (i128)(m - i) * K + m; }; auto getAp = [&](int i) -> i128 { return -(i128)P[i] + (i128)(m - i) * K + 2 * i - m; }; auto getCp = [&](int i) -> i128 { return -(i128)P[i] + (i128)(m - i) * K - m; }; // Decreasing pass: compute bestF and bestU2 for (int s = m; s >= 1; --s) { // F: max over i > s, i >= high_start while (stF.size() >= 2) { int i1 = stF.back(); int i2 = stF[stF.size() - 2]; i128 A1 = getA(i1), A2 = getA(i2); i128 c1 = 2 * i1 - m, c2 = 2 * i2 - m; if ((s + A1) * c2 <= (s + A2) * c1) stF.pop_back(); else break; } int i = s + 1; if (i <= m && i >= high_start) { if (stF.empty()) { stF.push_back(i); } else { int b = stF.back(); i128 Ai = getA(i), Ab = getA(b); i128 ci = 2 * i - m, cb = 2 * b - m; if ((s + Ai) * cb > (s + Ab) * ci) stF.push_back(i); } } bestF[s] = stF.empty() ? -1 : stF.back(); // U2: min over i > s, i <= low_end int k = s + 1; if (k <= low_end) { while ((int)hullU2.size() - headU2 >= 2) { int ii = hullU2[hullU2.size() - 2]; int jj = hullU2.back(); i128 di = m - 2 * ii, dj = m - 2 * jj, dk = m - 2 * k; i128 Ai = getAp(ii), Aj = getAp(jj), Ak = getAp(k); i128 left_num = di * Aj - dj * Ai; i128 left_den = dj - di; i128 right_num = dj * Ak - dk * Aj; i128 right_den = dk - dj; if (left_num * right_den >= right_num * left_den) hullU2.pop_back(); else break; } hullU2.push_back(k); } while ((int)hullU2.size() - headU2 >= 2) { int ii = hullU2[headU2]; int jj = hullU2[headU2 + 1]; i128 di = m - 2 * ii, dj = m - 2 * jj; i128 Ai = getAp(ii), Aj = getAp(jj); if ((s - Ai) * dj <= (s - Aj) * di) ++headU2; else break; } if ((int)hullU2.size() > headU2) bestU2[s] = hullU2[headU2]; else bestU2[s] = -1; } // Increasing pass: compute P, U1 and test each s hullP.clear(); int headP = 0; stU1.clear(); if (low_end >= 0) stU1.push_back(0); for (int s = 1; s <= m; ++s) { // P: max over i <= s, i >= high_start if (s >= high_start) { int k = s; while ((int)hullP.size() - headP >= 2) { int ii = hullP[hullP.size() - 2]; int jj = hullP.back(); i128 ci = 2 * ii - m, cj = 2 * jj - m, ck = 2 * k - m; i128 Ci = getC(ii), Cj = getC(jj), Ck = getC(k); i128 left_num = cj * Ci - ci * Cj; i128 left_den = cj - ci; i128 right_num = ck * Cj - cj * Ck; i128 right_den = ck - cj; if (left_num * right_den >= right_num * left_den) hullP.pop_back(); else break; } hullP.push_back(k); } while ((int)hullP.size() - headP >= 2) { int ii = hullP[headP]; int jj = hullP[headP + 1]; i128 ci = 2 * ii - m, cj = 2 * jj - m; i128 Ci = getC(ii), Cj = getC(jj); if ((-s + Ci) * cj <= (-s + Cj) * ci) ++headP; else break; } int bestP = ((int)hullP.size() > headP) ? hullP[headP] : -1; // U1: min over i <= s, i <= low_end while (stU1.size() >= 2) { int i1 = stU1.back(); int i2 = stU1[stU1.size() - 2]; i128 d1 = m - 2 * i1, d2 = m - 2 * i2; i128 Cp1 = getCp(i1), Cp2 = getCp(i2); if ((s + Cp1) * d2 >= (s + Cp2) * d1) stU1.pop_back(); else break; } if (s <= low_end) { int k = s; if (stU1.empty()) { stU1.push_back(k); } else { int b = stU1.back(); i128 dk = m - 2 * k, db = m - 2 * b; i128 Cpk = getCp(k), Cpb = getCp(b); if ((s + Cpk) * db < (s + Cpb) * dk) stU1.push_back(k); } } int bestU1 = stU1.empty() ? -1 : stU1.back(); // Middle index constraint (only if m even) bool mid_ok = true; if (mid_even != -1) { int i = mid_even; i128 val = (i128)(m - i) * K + 2 * max(0, i - s) - m + s; if (val < P[i]) mid_ok = false; } if (!mid_ok) continue; i128 qmin = 0; if (bestF[s] != -1) { int i = bestF[s]; i128 Ai = getA(i); i128 c = 2 * i - m; i128 q = ceil_div((i128)s + Ai, c); if (q > qmin) qmin = q; } if (bestP != -1) { int i = bestP; i128 Ci = getC(i); i128 c = 2 * i - m; i128 q = ceil_div(Ci - s, c); if (q > qmin) qmin = q; } i128 qmax = ((i128)1 << 120); if (bestU1 != -1) { int i = bestU1; i128 Cp = getCp(i); i128 d = m - 2 * i; i128 q = floor_div((i128)s + Cp, d); if (q < qmax) qmax = q; } if (bestU2[s] != -1) { int i = bestU2[s]; i128 Ap = getAp(i); i128 d = m - 2 * i; i128 q = floor_div(Ap - s, d); if (q < qmax) qmax = q; } if (qmin < 0) qmin = 0; long long maxq_ll = K - (s < m ? 1 : 0); if (maxq_ll < 0) continue; if (qmax > (i128)maxq_ll) qmax = maxq_ll; if (qmin <= qmax) return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; P.assign(N, 0); long long maxp = 0; for (int i = 0; i < N; ++i) { cin >> P[i]; if (P[i] > maxp) maxp = P[i]; } if (maxp == 0) { cout << 0 << '\n'; continue; } long long lo = -1; long long hi = 2 * maxp; // always feasible while (hi - lo > 1) { long long mid = lo + (hi - lo) / 2; if (feasible(N, mid)) hi = mid; else lo = mid; } cout << hi << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using i128 = __int128_t; struct Line { long long a; // numerator of slope i128 b; // numerator of intercept long long d; // positive denominator: value = (a*x + b) / d }; static inline i128 value_num(const Line& L, long long x) { return (i128)L.a * x + L.b; } static inline bool ge_at(const Line& L1, const Line& L2, long long x) { return value_num(L1, x) * L2.d >= value_num(L2, x) * L1.d; } static inline bool leq_at(const Line& L1, const Line& L2, long long x) { return value_num(L1, x) * L2.d <= value_num(L2, x) * L1.d; } static inline i128 ceil_div_i128(i128 n, i128 d) { if (n >= 0) return (n + d - 1) / d; return -((-n) / d); } static inline bool bad(const Line& l1, const Line& l2, const Line& l3) { i128 num12 = l1.b * l2.d - l2.b * l1.d; i128 den12 = (i128)l2.a * l1.d - (i128)l1.a * l2.d; i128 num23 = l2.b * l3.d - l3.b * l2.d; i128 den23 = (i128)l3.a * l2.d - (i128)l2.a * l3.d; if (den12 <= 0 || den23 <= 0) return false; return num12 * den23 >= num23 * den12; } static inline void add_std(vector<Line>& hull, Line L) { while (!hull.empty()) { if ((i128)hull.back().a * L.d == (i128)L.a * hull.back().d) { if (hull.back().b * L.d >= L.b * hull.back().d) return; hull.pop_back(); } else break; } while (hull.size() >= 2 && bad(hull[hull.size() - 2], hull.back(), L)) { hull.pop_back(); } hull.push_back(L); } struct Solver { int N, m; vector<long long> p; Solver(int n, vector<long long>& pp) : N(n), m(n - 1), p(pp) {} bool check(long long S) { vector<i128> L(m + 1); for (int i = 0; i <= m; ++i) { i128 x = (i128)p[i] + (i128)S * i; L[i] = (x + 1) / 2; // ceil(x / 2) } const long long INF = 4'000'000'000'000'000'000LL; vector<long long> qlow(m + 1, -INF), qhigh(m + 1, INF); // Pass 1: future lower bounds (c < 0, i > t) { vector<Line> st; st.reserve(m + 1); for (int t = m; t >= 1; --t) { int i = t + 1; if (i <= m) { int c = m - 2 * i; if (c < 0) { long long d = -c; i128 A = -(i128)S * m + 2 * L[i] - 2 * i + m; Line ln{1, A, d}; if (!st.empty() && leq_at(ln, st.back(), t)) { // discard } else { st.push_back(ln); } } } while (st.size() >= 2 && ge_at(st[st.size() - 2], st.back(), t)) st.pop_back(); if (!st.empty()) { i128 num = (i128)st.back().a * t + st.back().b; long long q = (long long)ceil_div_i128(num, st.back().d); if (q > qlow[t]) qlow[t] = q; } } } // Pass 2: future upper bounds (c > 0, i > t) { vector<Line> hull; hull.reserve(m + 1); int ptr = 0; for (int t = m; t >= 1; --t) { int i = t + 1; if (i <= m) { int c = m - 2 * i; if (c > 0) { long long d = c; i128 A = -(i128)S * m + 2 * L[i] - 2 * i + m; Line ln{-1, A, d}; add_std(hull, ln); } } long long y = -t; if (!hull.empty()) { if (ptr >= (int)hull.size()) ptr = (int)hull.size() - 1; while (ptr + 1 < (int)hull.size() && ge_at(hull[ptr + 1], hull[ptr], y)) ++ptr; i128 num = (i128)hull[ptr].a * y + hull[ptr].b; i128 ceilv = ceil_div_i128(num, hull[ptr].d); long long qh = -(long long)ceilv; if (qh < qhigh[t]) qhigh[t] = qh; } } } // Pass 3: past lower bounds (c < 0, i <= t) { vector<Line> hull; hull.reserve(m + 1); int ptr = 0; for (int t = 1; t <= m; ++t) { int i = t; int c = m - 2 * i; if (c < 0) { long long d = -c; i128 A = -(i128)S * m + 2 * L[i] + m; Line ln{-1, A, d}; add_std(hull, ln); } if (!hull.empty()) { if (ptr >= (int)hull.size()) ptr = (int)hull.size() - 1; while (ptr + 1 < (int)hull.size() && ge_at(hull[ptr + 1], hull[ptr], t)) ++ptr; i128 num = (i128)hull[ptr].a * t + hull[ptr].b; long long q = (long long)ceil_div_i128(num, hull[ptr].d); if (q > qlow[t]) qlow[t] = q; } } } // Pass 4: past upper bounds (c > 0, i <= t) { vector<Line> st; st.reserve(m + 1); for (int t = 1; t <= m; ++t) { if (t == 1) { int i = 0; int c = m - 2 * i; if (c > 0) { long long d = c; i128 A = -(i128)S * m + 2 * L[i] + m; Line ln{1, A, d}; long long y = -t; if (!st.empty() && leq_at(ln, st.back(), y)) { // discard } else { st.push_back(ln); } } } int i = t; int c = m - 2 * i; if (c > 0) { long long d = c; i128 A = -(i128)S * m + 2 * L[i] + m; Line ln{1, A, d}; long long y = -t; if (!st.empty() && leq_at(ln, st.back(), y)) { // discard } else { st.push_back(ln); } } long long y = -t; while (st.size() >= 2 && ge_at(st[st.size() - 2], st.back(), y)) st.pop_back(); if (!st.empty()) { i128 num = (i128)st.back().a * y + st.back().b; i128 ceilv = ceil_div_i128(num, st.back().d); long long qh = -(long long)ceilv; if (qh < qhigh[t]) qhigh[t] = qh; } } } // c == 0 constraints bool hasZero = (m % 2 == 0); int i0 = m / 2; i128 zero_low = 0, zero_high = 0; if (hasZero) { zero_low = 2 * L[i0] + m - (i128)S * m; zero_high = (i128)S * m - 2 * L[i0]; } for (int t = 1; t <= m; ++t) { if (hasZero) { if (t >= i0) { if ((i128)t < zero_low) continue; } else { if ((i128)t > zero_high) continue; } } long long lo = qlow[t]; long long hi = qhigh[t]; if (lo < 0) lo = 0; long long qmax = (t == m ? S : S - 1); if (hi > qmax) hi = qmax; if (lo <= hi) return true; } return false; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<long long> p(N); for (int i = 0; i < N; ++i) cin >> p[i]; Solver solver(N, p); long long lo = 0; long long hi = 4'000'000'000'000'000'000LL; while (lo < hi) { long long mid = lo + (hi - lo) / 2; if (solver.check(mid)) hi = mid; else lo = mid + 1; } cout << lo << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; i128 floor_div(i128 a, i128 b) { if (a >= 0) return a / b; return - ((-a + b - 1) / b); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); int64 maxp = 0; for (int i = 0; i < N; ++i) { cin >> p[i]; maxp = max(maxp, p[i]); } int M = N - 1; auto check = [&](int64 S) -> bool { if (S == 0) { for (int i = 0; i < N; ++i) if (p[i] != 0) return false; return true; } int64 s = S & 1; int64 K = (S - s) / 2; i128 Hmin = -(i128)M * (K + s); i128 Hmax = (i128)M * K; auto G = [&](i128 H) -> i128 { i128 q = floor_div(H, M); i128 r = H - q * M; i128 MK = (i128)M * K; i128 min_diff = ((i128)1 << 120); for (int i = 0; i <= M; ++i) { i128 F = MK + q * (2LL * i - M) - r + (i128)i * s; i128 extra = (i128)i + r - M; if (extra > 0) F += 2 * extra; i128 diff = F - p[i]; if (diff < min_diff) min_diff = diff; } return min_diff; }; i128 l = Hmin, r = Hmax; while (r - l > 3) { i128 m1 = l + (r - l) / 3; i128 m2 = r - (r - l) / 3; i128 g1 = G(m1); i128 g2 = G(m2); if (g1 < g2) l = m1; else r = m2; } i128 best = -(((i128)1) << 120); for (i128 H = l; H <= r; ++H) { i128 g = G(H); if (g > best) best = g; } return best >= 0; }; int64 lo = 0, hi = 2 * maxp; // ensure hi is feasible while (!check(hi)) { hi *= 2; if (hi == 0) hi = 1; } while (lo < hi) { int64 mid = lo + (hi - lo) / 2; if (check(mid)) hi = mid; else lo = mid + 1; } cout << lo << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; static i128 ceilDiv(i128 a, long long b) { if (a >= 0) return (a + b - 1) / b; return a / b; } static i128 floorDiv(i128 a, long long b) { if (a >= 0) return a / b; return -((-a + b - 1) / b); } struct Fraction { i128 num; long long den; }; static int compareFractions(const Fraction& x, const Fraction& y) { i128 lhs = x.num * y.den; i128 rhs = y.num * x.den; return (lhs > rhs) - (lhs < rhs); } /* Represents the line y = (a*x + b) / d, d > 0. All hulls used in this solution insert lines in strictly decreasing slope order. The query direction is fixed for each hull. */ struct Line { i128 b; int64 a, d; Line(int64 a_, i128 b_, int64 d_) : b(b_), a(a_), d(d_) {} }; class MonotoneHull { bool maximum; vector<Line> lines; size_t head = 0; static int compareAt(const Line& x, const Line& y, int64 at) { i128 nx = x.b + (i128)x.a * at; i128 ny = y.b + (i128)y.a * at; i128 lhs = nx * y.d; i128 rhs = ny * x.d; return (lhs > rhs) - (lhs < rhs); } // x12 is the intersection of l1,l2; x23 is the intersection of l2,l3. bool redundant(const Line& l1, const Line& l2, const Line& l3) const { i128 n12 = l2.b * l1.d - l1.b * l2.d; i128 n23 = l3.b * l2.d - l2.b * l3.d; int64 q12 = l1.a * l2.d - l2.a * l1.d; int64 q23 = l2.a * l3.d - l3.a * l2.d; // Slopes are decreasing, so q12 and q23 are positive. if (maximum) { // For a maximum hull, x12 must be greater than x23. return n12 * q23 <= n23 * q12; } else { // For a minimum hull with decreasing slopes, x12 < x23. return n12 * q23 >= n23 * q12; } } public: MonotoneHull(bool maximum_, size_t capacity = 0) : maximum(maximum_) { lines.reserve(capacity); } bool empty() const { return lines.size() == head; } void add(const Line& line) { while (lines.size() >= head + 2) { size_t n = lines.size(); if (!redundant(lines[n - 2], lines[n - 1], line)) break; lines.pop_back(); } lines.push_back(line); } // Query x values arrive in increasing order. Fraction queryIncreasing(int64 x) { if (maximum) { while (lines.size() >= head + 2) { size_t n = lines.size(); if (compareAt(lines[n - 2], lines[n - 1], x) < 0) break; lines.pop_back(); } const Line& line = lines.back(); return {line.b + (i128)line.a * x, line.d}; } else { while (lines.size() >= head + 2) { if (compareAt(lines[head + 1], lines[head], x) > 0) break; ++head; } const Line& line = lines[head]; return {line.b + (i128)line.a * x, line.d}; } } // Query x values arrive in decreasing order. Fraction queryDecreasing(int64 x) { if (maximum) { while (lines.size() >= head + 2) { if (compareAt(lines[head + 1], lines[head], x) < 0) break; ++head; } const Line& line = lines[head]; return {line.b + (i128)line.a * x, line.d}; } else { while (lines.size() >= head + 2) { size_t n = lines.size(); if (compareAt(lines[n - 2], lines[n - 1], x) > 0) break; lines.pop_back(); } const Line& line = lines.back(); return {line.b + (i128)line.a * x, line.d}; } } }; /* Check whether K punches can break everything using only tiles 0 and L. q punches are placed at L, and K-q punches are placed at 0. */ static bool endpointFeasible(const vector<int64>& p, int64 K) { int L = (int)p.size() - 1; i128 lower = 0; i128 upper = K; for (int i = 0; i <= L; ++i) { if (2LL * i < L) { long long d = L - 2 * i; i128 numerator = (i128)p[i] - (i128)K * i; i128 needed = ceilDiv(numerator, d); if (needed > lower) lower = needed; } else if (2LL * i > L) { long long d = 2 * i - L; i128 numerator = (i128)K * i - p[i]; i128 allowed = floorDiv(numerator, d); if (allowed < upper) upper = allowed; } else { if ((i128)K * i < p[i]) return false; } if (lower > upper) return false; } return lower <= upper; } /* Check whether K punches suffice when at most one punch is at an interior tile. For a remainder r: r = 0: q punches at L and K-q punches at 0. r > 0: q punches at L, one punch at r, and K-q-1 punches at 0. */ static bool interiorFeasible(const vector<int64>& p, int64 K) { int L = (int)p.size() - 1; if (K == 0) { for (int64 x : p) { if (x != 0) return false; } return true; } int positiveCount = (L + 1) / 2; int negativeCount = L - positiveCount; MonotoneHull leftPrefix(true, positiveCount); MonotoneHull leftSuffix(true, positiveCount); MonotoneHull rightSuffix(false, negativeCount); MonotoneHull rightPrefix(false, negativeCount); vector<Fraction> lower(L), upper(L); for (int r = 0; r < L; ++r) { lower[r] = {0, 1}; upper[r] = {(i128)K, 1}; } // Lower bounds from left-side tiles with i <= r. for (int r = 0; r < L; ++r) { if (2LL * r < L) { long long d = L - 2 * r; i128 b = (i128)p[r] - ((i128)K - 2) * r; leftPrefix.add(Line(-1, b, d)); } Fraction value = leftPrefix.queryIncreasing(r); if (compareFractions(value, lower[r]) > 0) lower[r] = value; } // Lower bounds from left-side tiles with i >= r. for (int r = L - 1; r >= 0; --r) { if (2LL * r < L) { long long d = L - 2 * r; i128 b = (i128)p[r] - (i128)K * r; leftSuffix.add(Line(1, b, d)); } if (!leftSuffix.empty()) { Fraction value = leftSuffix.queryDecreasing(r); if (compareFractions(value, lower[r]) > 0) lower[r] = value; } } // Upper bounds from right-side tiles with i >= r. // Start at i=L because tile L belongs to this suffix for every r. for (int r = L; r >= 0; --r) { if (2LL * r > L) { long long d = 2 * r - L; i128 b = (i128)K * r - p[r]; rightSuffix.add(Line(-1, b, d)); } if (r < L && !rightSuffix.empty()) { Fraction value = rightSuffix.queryDecreasing(r); if (compareFractions(value, upper[r]) < 0) upper[r] = value; } } // Upper bounds from right-side tiles with i <= r. for (int r = 0; r < L; ++r) { if (2LL * r > L) { long long d = 2 * r - L; i128 b = ((i128)K - 2) * r - p[r]; rightPrefix.add(Line(1, b, d)); } if (!rightPrefix.empty()) { Fraction value = rightPrefix.queryIncreasing(r); if (compareFractions(value, upper[r]) < 0) upper[r] = value; } } for (int r = 0; r < L; ++r) { // If L is even, the middle tile has no q term. if (L % 2 == 0) { int middle = L / 2; i128 power; if (r < middle) { power = (i128)K * middle - r; } else { power = ((i128)K - 2) * middle + r; } if (power < p[middle]) continue; } i128 smallestQ = ceilDiv(lower[r].num, lower[r].den); i128 largestQ = floorDiv(upper[r].num, upper[r].den); i128 cap = (r == 0 ? (i128)K : (i128)K - 1); if (largestQ > cap) largestQ = cap; if (smallestQ <= largestQ) return true; } return false; } static int64 solve(const vector<int64>& p) { int L = (int)p.size() - 1; int64 maxP = *max_element(p.begin(), p.end()); // M punches at each endpoint give every tile at least M*L power. int64 M = (maxP + L - 1) / L; // Necessary lower bounds for endpoint-only solutions. int64 low = (int64)(((i128)p[0] + p[L] + L - 1) / L); for (int i = 0; i <= L; ++i) { int64 maxDistance = max(i, L - i); int64 needed = (p[i] + maxDistance - 1) / maxDistance; low = max(low, needed); } // A potentially tighter endpoint-only feasible upper bound. int64 A = (p[L] + L - 1) / L; // punches at 0 int64 B = (p[0] + L - 1) / L; // punches at L i128 maxDeficit = 0; for (int i = 0; i <= L; ++i) { i128 base = (i128)A * i + (i128)B * (L - i); maxDeficit = max(maxDeficit, (i128)p[i] - base); } int64 extra = (int64)((maxDeficit + L - 1) / L); i128 candidate = (i128)A + B + 2 * extra; i128 high128 = min((i128)2 * M, candidate); int64 high = (int64)high128; // Find the minimum using only endpoint punches. while (low < high) { int64 mid = low + (high - low) / 2; if (endpointFeasible(p, mid)) { high = mid; } else { low = mid + 1; } } int64 endpoint optimum = low; if (endpointOptimum == 0) return 0; // The unrestricted optimum is either E or E-1. if (interiorFeasible(p, endpointOptimum - 1)) { return endpointOptimum - 1; } return endpointOptimum; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); for (int64& x : p) cin >> x; cout << solve(p) << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; static i128 ceilDiv(i128 a, int64 b) { i128 q = a / b; i128 r = a % b; if (r > 0) ++q; return q; } static i128 floorDiv(i128 a, int64 b) { i128 q = a / b; i128 r = a % b; if (r < 0) --q; return q; } /* A line represents the rational function (slope * x + b) / den, den > 0. The Li Chao tree maintains either the maximum or the minimum. */ class LiChao { struct Line { i128 b; int slope; int den; }; int n; bool maximize; vector<Line> lines; vector<int> tree; bool better(int id1, int id2, int x) const { const Line &a = lines[id1]; const Line &b = lines[id2]; i128 lhs = (a.b + (i128)a.slope * x) * b.den; i128 rhs = (b.b + (i128)b.slope * x) * a.den; return maximize ? lhs > rhs : lhs < rhs; } void insert(int id, int node, int l, int r) { if (tree[node] == -1) { tree[node] = id; return; } int mid = (l + r) / 2; bool leftBetter = better(id, tree[node], l); bool midBetter = better(id, tree[node], mid); if (midBetter) swap(id, tree[node]); if (l == r) return; if (leftBetter != midBetter) { insert(id, node * 2, l, mid); } else { insert(id, node * 2 + 1, mid + 1, r); } } public: struct Bound { i128 num = 0; int den = 0; // den == 0 means empty }; LiChao(int domainSize, bool findMaximum) : n(domainSize), maximize(findMaximum), tree(4 * domainSize + 5, -1) { lines.reserve(domainSize); } void clear() { lines.clear(); fill(tree.begin(), tree.end(), -1); } void add(int slope, i128 b, int den) { lines.push_back({b, slope, den}); insert((int)lines.size() - 1, 1, 0, n - 1); } Bound query(int x) const { int node = 1, l = 0, r = n - 1; int best = -1; while (true) { int id = tree[node]; if (id == -1) break; if (best == -1 || better(id, best, x)) best = id; if (l == r) break; int mid = (l + r) / 2; if (x <= mid) { node *= 2; r = mid; } else { node = node * 2 + 1; l = mid + 1; } } if (best == -1) return {}; return { lines[best].b + (i128)lines[best].slope * x, lines[best].den }; } }; static bool fractionGreater(const LiChao::Bound &a, const LiChao::Bound &b) { return a.num * b.den > b.num * a.den; } static bool fractionLess(const LiChao::Bound &a, const LiChao::Bound &b) { return a.num * b.den < b.num * a.den; } /* Is there a fractional endpoint solution with exactly C total punches? u punches at 0, v punches at L=N-1, with u+v=C. Equivalently, u=C-v. */ static bool fractionalEndpointFeasible(int64 C, const vector<int64> &p, int L) { i128 lowNum = 0; int lowDen = 1; i128 highNum = C; int highDen = 1; for (int i = 0; i <= L; ++i) { int d = L - 2 * i; if (d > 0) { // v >= (p[i] - C*i) / d i128 num = (i128)p[i] - (i128)C * i; if (num * lowDen > lowNum * d) { lowNum = num; lowDen = d; } } else if (d < 0) { // v <= (C*i - p[i]) / (2*i-L) i128 num = (i128)C * i - p[i]; int den = -d; if (num * highDen < highNum * den) { highNum = num; highDen = den; } } else { if ((i128)C * i < p[i]) return false; } } return lowNum * highDen <= highNum * lowDen; } /* Quick check using only endpoint punches. q punches are at L and C-q punches are at 0. */ static bool integerEndpointFeasible(int64 C, const vector<int64> &p, int L) { i128 low = 0; i128 high = C; for (int i = 0; i <= L; ++i) { int d = L - 2 * i; if (d > 0) { i128 need = ceilDiv((i128)p[i] - (i128)C * i, d); low = max(low, need); } else if (d < 0) { i128 bound = floorDiv((i128)C * i - p[i], -d); high = min(high, bound); } else { if ((i128)C * i < p[i]) return false; } } return low <= high; } /* Check whether k punches suffice. By the canonicalization argument, it is enough to consider: - q ordinary punches at L, - one special punch at r, - k-1-q ordinary punches at 0, where 0 <= q <= k-1 and 0 <= r <= L. Their power at tile i is (k-1)*i + q*(L-2*i) + abs(i-r). */ static bool canonicalFeasible(int64 k, const vector<int64> &p, int L) { int N = L + 1; if (integerEndpointFeasible(k, p, L)) return true; LiChao maxLeft(N, true); LiChao minRight(N, false); vector<LiChao::Bound> prefixMax(N), prefixMin(N); i128 km2 = (i128)k - 2; i128 kk = (i128)k; // Prefix envelopes: indices i <= r. for (int r = 0; r <= L; ++r) { int i = r; if (2 * i < L) { int den = L - 2 * i; i128 b = (i128)p[i] - km2 * i; maxLeft.add(-1, b, den); } else if (2 * i > L) { int den = 2 * i - L; i128 b = km2 * i - p[i]; minRight.add(1, b, den); } prefixMax[r] = maxLeft.query(r); prefixMin[r] = minRight.query(r); } maxLeft.clear(); minRight.clear(); // Suffix envelopes: indices i > r. for (int r = L; r >= 0; --r) { LiChao::Bound suffixMax = maxLeft.query(r); LiChao::Bound suffixMin = minRight.query(r); // Add tile r to the suffix for the next iteration. int i = r; if (2 * i < L) { int den = L - 2 * i; i128 b = (i128)p[i] - kk * i; maxLeft.add(1, b, den); } else if (2 * i > L) { int den = 2 * i - L; i128 b = kk * i - p[i]; minRight.add(-1, b, den); } LiChao::Bound lowerBound = prefixMax[r]; if (suffixMax.den != 0 && (lowerBound.den == 0 || fractionGreater(suffixMax, lowerBound))) { lowerBound = suffixMax; } LiChao::Bound upperBound = prefixMin[r]; if (suffixMin.den != 0 && (upperBound.den == 0 || fractionLess(suffixMin, upperBound))) { upperBound = suffixMin; } if (lowerBound.den == 0 || upperBound.den == 0) continue; // The middle tile imposes a condition independent of q. if (L % 2 == 0) { int mid = L / 2; i128 power = (i128)(k - 1) * mid + abs(mid - r); if (power < p[mid]) continue; } i128 low = ceilDiv(lowerBound.num, lowerBound.den); i128 high = floorDiv(upperBound.num, upperBound.den); if (low < 0) low = 0; if (high > (i128)k - 1) high = (i128)k - 1; if (low <= high) return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); int64 maxP = 0; for (int i = 0; i < N; ++i) { cin >> p[i]; maxP = max(maxP, p[i]); } if (maxP == 0) { cout << 0 << '\n'; continue; } int L = N - 1; // Lower bound: one punch contributes at most max(i, L-i). int64 low = 0; for (int i = 0; i < N; ++i) { int64 maxDistance = max(i, L - i); int64 need = (p[i] + maxDistance - 1) / maxDistance; low = max(low, need); } // u=v=maxP is always a feasible fractional endpoint solution. int64 high = 2 * maxP; while (low < high) { int64 mid = low + (high - low) / 2; if (fractionalEndpointFeasible(mid, p, L)) { high = mid; } else { low = mid + 1; } } int64 K = low; if (canonicalFeasible(K, p, L)) { cout << K << '\n'; } else { cout << K + 1 << '\n'; } } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; using i128 = __int128_t; /* A line represents f(x) = (a*x + b) / den, den > 0. Slopes are always inserted in increasing order. */ struct Line { i128 b; int a; int den; }; struct Fraction { i128 num; int den; }; static i128 ceil_div(i128 a, i128 b) { if (a >= 0) return (a + b - 1) / b; return a / b; } static i128 floor_div(i128 a, i128 b) { if (a >= 0) return a / b; return -((-a + b - 1) / b); } /* Monotone convex hull. isMax = true: maximum envelope isMax = false: minimum envelope pointerMode = true: Queries move toward lines with larger slopes. pointerMode = false: Queries move toward lines with smaller slopes, so dominated lines can be permanently removed from the back. */ class MonotoneHull { bool isMax; bool pointerMode; vector<Line> hull; size_t ptr = 0; static bool value_ge(const Line& x, const Line& y, int pos) { i128 nx = (i128)x.a * pos + x.b; i128 ny = (i128)y.a * pos + y.b; return nx * y.den >= ny * x.den; } static bool value_le(const Line& x, const Line& y, int pos) { i128 nx = (i128)x.a * pos + x.b; i128 ny = (i128)y.a * pos + y.b; return nx * y.den <= ny * x.den; } // Intersection of consecutive lines, with positive denominator. static void intersection(const Line& x, const Line& y, i128& numerator, i128& denominator) { denominator = (i128)y.a * x.den - (i128)x.a * y.den; numerator = x.b * y.den - y.b * x.den; } static bool bad_max(const Line& x, const Line& y, const Line& z) { i128 n12, d12, n23, d23; intersection(x, y, n12, d12); intersection(y, z, n23, d23); // The middle line is unnecessary if intersection(x,y) >= intersection(y,z). return n12 * d23 >= n23 * d12; } static bool bad_min(const Line& x, const Line& y, const Line& z) { i128 n12, d12, n23, d23; intersection(x, y, n12, d12); intersection(y, z, n23, d23); // The middle line is unnecessary if intersection(y,z) >= intersection(x,y). return n23 * d12 >= n12 * d23; } public: MonotoneHull(bool maximum, bool use_pointer) : isMax(maximum), pointerMode(use_pointer) {} void reserve(int n) { hull.reserve(n); } void clear() { hull.clear(); ptr = 0; } bool empty() const { return hull.empty(); } void add(Line line) { while (hull.size() >= 2) { bool bad = isMax ? bad_max(hull[hull.size() - 2], hull.back(), line) : bad_min(hull[hull.size() - 2], hull.back(), line); if (!bad) break; hull.pop_back(); } hull.push_back(line); if (pointerMode && ptr >= hull.size()) { ptr = hull.size() - 1; } } const Line& get(int x) { if (pointerMode) { while (ptr + 1 < hull.size()) { bool next_better = isMax ? value_ge(hull[ptr + 1], hull[ptr], x) : value_le(hull[ptr + 1], hull[ptr], x); if (!next_better) break; ++ptr; } return hull[ptr]; } while (hull.size() >= 2) { const Line& previous = hull[hull.size() - 2]; const Line& last = hull.back(); bool remove_last = isMax ? value_ge(previous, last, x) : value_le(previous, last, x); if (!remove_last) break; hull.pop_back(); } return hull.back(); } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int64> p(N); int64 maximumPower = 0; for (int i = 0; i < N; ++i) { cin >> p[i]; maximumPower = max(maximumPower, p[i]); } if (maximumPower == 0) { cout << 0 << '\n'; continue; } const int L = N - 1; /* Necessary lower bounds: 1. A punch contributes at most max(i, L-i) to tile i. 2. The total power at the two endpoints is exactly K*L. */ int64 low = 1; for (int i = 0; i < N; ++i) { int64 maximumDistance = max(i, L - i); int64 needed = (p[i] + maximumDistance - 1) / maximumDistance; low = max(low, needed); } i128 endpointSum = (i128)p[0] + p[L]; low = max(low, (int64)((endpointSum + L - 1) / L)); /* M punches at tile 0 and M punches at tile L give every tile M*i + M*(L-i) = M*L power. */ int64 M = (maximumPower + L - 1) / L; int64 high = 2 * M; vector<Fraction> prefixF(L + 1); vector<Fraction> prefixG(L + 1); MonotoneHull fPrefix(true, true); // Maximum, pointer queries. MonotoneHull gPrefix(false, false); // Minimum, popping queries. MonotoneHull fSuffix(true, false); // Maximum, popping queries. MonotoneHull gSuffix(false, true); // Minimum, pointer queries. fPrefix.reserve(L + 1); gPrefix.reserve(L + 1); fSuffix.reserve(L + 1); gSuffix.reserve(L + 1); auto feasible = [&](int64 K) -> bool { i128 m = (i128)K - 1; fill(prefixF.begin(), prefixF.end(), Fraction{0, 0}); fill(prefixG.begin(), prefixG.end(), Fraction{0, 0}); fPrefix.clear(); gPrefix.clear(); fSuffix.clear(); gSuffix.clear(); // Prefix envelopes: indices i <= x. for (int x = 0; x <= L; ++x) { int coefficient = 2 * x - L; if (coefficient > 0) { i128 c = (i128)p[x] - m * (L - x); fPrefix.add(Line{c + x, -1, coefficient}); } else if (coefficient < 0) { i128 d = m * (L - x) - p[x]; gPrefix.add(Line{d - x, 1, -coefficient}); } if (!fPrefix.empty()) { const Line& line = fPrefix.get(x); i128 num = (i128)line.a * x + line.b; Fraction& best = prefixF[x]; if (best.den == 0 || num * best.den > best.num * line.den) { best = Fraction{num, line.den}; } } if (!gPrefix.empty()) { const Line& line = gPrefix.get(x); i128 num = (i128)line.a * x + line.b; Fraction& best = prefixG[x]; if (best.den == 0 || num * best.den < best.num * line.den) { best = Fraction{num, line.den}; } } } // Suffix envelopes: indices i >= x. for (int x = L; x >= 0; --x) { int coefficient = 2 * x - L; Fraction F = prefixF[x]; Fraction G = prefixG[x]; if (coefficient > 0) { i128 c = (i128)p[x] - m * (L - x); fSuffix.add(Line{c - x, 1, coefficient}); const Line& line = fSuffix.get(x); i128 num = (i128)line.a * x + line.b; if (F.den == 0 || num * F.den > F.num * line.den) { F = Fraction{num, line.den}; } } else if (coefficient < 0) { i128 d = m * (L - x) - p[x]; gSuffix.add(Line{d + x, -1, -coefficient}); const Line& line = gSuffix.get(x); i128 num = (i128)line.a * x + line.b; if (G.den == 0 || num * G.den < G.num * line.den) { G = Fraction{num, line.den}; } } if (F.den == 0 || G.den == 0) continue; // If L is even, the middle tile gives no bound on q. if (L % 2 == 0) { int middle = L / 2; i128 power = m * middle + abs(middle - x); if ((i128)p[middle] > power) continue; } i128 minimumQ = ceil_div(F.num, F.den); i128 maximumQ = floor_div(G.num, G.den); if (minimumQ < 0) minimumQ = 0; if (maximumQ > m) maximumQ = m; if (minimumQ <= maximumQ) return true; } return false; }; while (low < high) { int64 mid = low + (high - low) / 2; if (feasible(mid)) { high = mid; } else { low = mid + 1; } } cout << low << '\n'; } return 0; }