a540: 10684 - The jackpot

Tags: dp, kadane, sequence


出處:https://zerojudge.tw/ShowProblem?problemid=a540
提交:https://zerojudge.tw/Submissions?problemid=a540&account=allllllan123456


問題:給定 $N\in[1,10000]$ 個絕對值小於 1000 的整數 arr[1:N],試求最大的非空連續子序列和?並判斷是否為正。


解法:這題與 d784 非常類似,可以直接參考該題作法。


 1#include <bits/stdc++.h>
 2using namespace std;
 3
 4int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); // IO 優化
 5    int n;
 6    while (cin >> n && n) {
 7        int sum=0, ans=INT_MIN;
 8        while (n--) {
 9            int a; cin >> a;
10            sum = max(sum + a, a);
11            if (sum > ans) ans = sum;
12        }
13        if (ans > 0) cout << "The maximum winning streak is " << ans << ".\n";
14        else cout << "Losing streak.\n";
15    }
16    return 0;
17}

no image