Topic Link: Click to open the link
Problem Solving Ideas:
It's a classic question. First of all, consider the details, when the sequence is 0 o'clock, obviously the final output 0, when the sequence is negative, it is obvious to output the maximum number.
After the details have been processed, you can return to normal orbit. We open two variables, save the current sequence and the previous maximum value, we update the current sequence and the condition is that if the current sequence and is negative, then we must update, otherwise it will definitely reduce the final result. The previous maximum value can also be updated during the update process.
Full code:
#include <iostream> #include <cstdio> #include <climits>using namespace std; typedef long LONG ll;int n;const int maxn = 1000001; LL A[MAXN]; ll Max (ll A, ll b) {return a > B? a:b;} void Solve () {LL maxpre = 0, Maxnow = 0; for (int i = 0; i < n; i + +) {Maxnow = Maxnow + a[i]; if (Maxnow < 0) {Maxnow = 0; } if (Maxnow > Maxpre) maxpre = Maxnow; } cout << maxpre << Endl;} int main () {#ifdef Doubleq freopen ("In.txt", "R", stdin); #endif//Doubleq while (CIN >> N) {int flag = 0, Flag2 = 0; LL Maxx = Long_min; for (int i = 0; i < n; i + +) {cin >> a[i]; if (a[i]! = 0) flag = 1; if (A[i] >= 0) Flag2 = 1; Maxx = Max (Maxx, A[i]); } if (flag = = 0) {cout << "0" << Endl; Continue } if (Flag2 = = 0) {cout << maxx << Endl; Continue } solve (); } return 0;}
Nine degrees 1077 (maximum sequence and)