rt,无意中发现了这种做法:
#include <bits/stdc++.h>
#define JS ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
using namespace std;
int n, x, mx, sum[100001], T, t;
int main() {
JS;
cin >> T;
while (T--) {
mx = -0x3f3f3f, t = 0;
memset(sum, 0, sizeof(sum));
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> sum[i];
t += sum[i];
t = max(0, t);
mx = max(mx, t);
}
cout << mx << '\n';
}
return 0;
}
O(n) 本地评测可过,正确性如何证明?