(1)有dp【i】【1/0】表示选和不选
(2)利用前缀和和map
(3关于重叠问题,定l和r)
这里
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF = 1e18;
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
vector<int> pre(n + 1);
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + a[i];
}
vector<tuple<int, int, int>> intervals; // sum, l, r
for (int l = 1; l <= n; l++) {
for (int r = l; r <= n; r++) {
intervals.emplace_back(pre[r] - pre[l - 1], l, r);
}
}
sort(intervals.begin(), intervals.end());
int min_diff = INF;
int m = intervals.size();
for (int i = 0; i < m; i++) {
auto [s1, l1, r1] = intervals[i];
for (int j = i + 1; j < m; j++) {
auto [s2, l2, r2] = intervals[j];
if (s2 - s1 >= min_diff) break; // 后续的差值会更大,无需继续检查
if (r1 < l2 || r2 < l1) { // 检查区间是否不重叠
min_diff = min(min_diff, s2 - s1);
}
}
}
cout << min_diff << "\n";
return 0;
}