AcWing 5990. 拔河
原题链接
中等
作者:
来杯whiskey
,
2025-04-03 21:43:34
· 四川
,
所有人可见
,
阅读 16
#include <bits/stdc++.h>
using i64 = long long;
std::multiset<i64> s;
int main()
{
int n;
std::cin >> n;
std::vector<i64> a(n+10);
for (int i = 1; i <= n; i ++) std::cin >> a[i], a[i] += a[i-1];
i64 res = 1e9;
for (int l = 1; l <= n; l ++) {
for (int i = 1; i <= l-1; i ++) {
s.insert(a[l-1] - a[i-1]);
}
for (int r = l; r <= n; r ++) {
i64 t = a[r] - a[l-1];
auto k = s.lower_bound(t);
if (k != s.end()) res = std::min(res, *k - t);
if (k != s.begin()) {
k --;
res = std::min(res, t - *k);
}
}
}
std::cout << res << "\n";
return 0;
}
#include <bits/stdc++.h>
using i64 = long long;
std::multiset<i64> s;
int main()
{
int n;
std::cin >> n;
std::vector<i64> a(n+10);
for (int i = 1; i <= n; i ++) std::cin >> a[i], a[i] += a[i-1];
for (int i = 1; i <= n; i ++) {
for (int j = i; j <= n; j ++) {
s.insert(a[j] - a[i-1]);
}
}
i64 res = 1e9;
for (int l = 1; l < n; l ++) {
for (int j = l; j <= n; j ++) {
auto t = s.find(a[j] - a[l-1]);
s.erase(t);
}
for (int i = 1; i <= l; i ++) {
i64 t = a[l] - a[i-1];
auto k = s.lower_bound(t);
if (k != s.end()) res = std::min(res, *k - t);
if (k != s.begin()) {
k --;
res = std::min(res, t - *k);
}
}
}
std::cout << res << "\n";
return 0;
}