思路:最大连续子段和:判断前缀和是否大于0,如果大于0对后面有贡献,否则置0.
#include <cstdio> #include <iostream> #include <cstdio> #include <algorithm> #include <functional> #include <set> #include <vector> #include <queue> #include <cstring> #include <stack> #include <climits> using namespace std; #define ll long long #define pb push_back #define fi first #define se second void solve(){ int T; cin >> T; while(T--){ int n; cin >> n; vector<int > a(n); ll sum = 0; for(auto& x : a){ cin >> x; sum += x; } vector<ll > dp(n + 1); ll Max = -1e9 * 1e5 - 1e9; for(int i = 1; i < n; ++i){ if(dp[i - 1] + a[i - 1] > 0) dp[i] = dp[i - 1] + a[i - 1]; else dp[i] = 0; Max = max(Max, dp[i]); } fill(dp.begin(), dp.end(), 0); for(int i = 2; i <= n; ++i){ if(dp[i - 1] + a[i - 1] > 0) dp[i] = dp[i - 1] + a[i - 1]; else dp[i] = 0; Max = max(Max, dp[i]); } if(sum > Max) cout << "yes" << endl; else cout << "no" << endl; } } int main(){ // freopen("C:\Users\admin\Desktop\input.txt", "r", stdin); // freopen("C:\Users\admin\Desktop\output.txt", "w", stdout); ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); return 0; }