Nine degrees 1077 (maximum sequence and)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.