49 lines
1.2 KiB
C++
49 lines
1.2 KiB
C++
#include <bits/stdc++.h>
|
|
using namespace std;
|
|
typedef long long LL;
|
|
const int N = 50010;
|
|
struct Node {
|
|
LL sum; // 前缀和
|
|
int id; // 到第几个
|
|
} s[N];
|
|
int n;
|
|
|
|
int main() {
|
|
// 配置从文本文件读入数据
|
|
freopen("4.in", "r", stdin);
|
|
scanf("%d", &n);
|
|
LL x;
|
|
LL ans = 0x3f3f3f3f; // 答案
|
|
|
|
for (int i = 1; i <= n; i++) {
|
|
scanf("%lld", &x);
|
|
s[i].sum = s[i - 1].sum + x; // 前缀和
|
|
s[i].id = i; // 序号
|
|
}
|
|
// O(N^2)暴力解法
|
|
// 遍历前缀和数组的每一个数据
|
|
// 50000 * 50000 = 2500000000 = 2.5 * 1e9
|
|
/**
|
|
1 49999
|
|
2 49998
|
|
3 49997
|
|
...
|
|
49999 1
|
|
|
|
(1+n-1)*(n-1)/2 = O(N^2/2) = 2.5 * 1e9/2 = 1.25 *1e9
|
|
*/
|
|
for (int i = 1; i < n; i++) // 50000
|
|
// 遍历每一个数据,找到最小的差值
|
|
for (int j = i + 1; j <= n; j++) { //
|
|
// 差值
|
|
LL d = s[j].sum - s[i].sum;
|
|
// 如果差值小于当前最小值,则更新最小值
|
|
if (d > 0 && d < ans)
|
|
ans = d;
|
|
}
|
|
|
|
// 输出答案
|
|
cout << ans << endl;
|
|
return 0;
|
|
}
|