AcWing 5990. 拔河
原题链接
中等
作者:
AC-player
,
2025-03-24 20:16:24
·天津
,
所有人可见
,
阅读 11
multiset有序可重复集合(多重集合)
#include<iostream>
#include<algorithm>
#include<set>
typedef long long LL;
using namespace std;
const int N = 1010;
LL a[N];
multiset<LL> ranges;
int n;
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++ )
{
cin >> a[i];
a[i] += a[i - 1];
}
ranges.insert(1e13), ranges.insert(-1e13);
LL res = 1e9;
for(int l = 1; l <= n; l ++ )
{
for(int i = 1; i <= l - 1; i ++)
{
ranges.insert(a[l - 1] - a[i - 1]);
}
for(int r = l; r <= n; r ++)
{
LL sum = a[r] - a[l - 1];
auto it = ranges.lower_bound(sum);
res = min(res, *it - sum);
it --;
res = min(res, sum - *it);
}
}
cout << res << endl;
return 0;
}