HDU 1003 Max Sum max continuous and partition method

Source: Internet
Author: User

Test instructions: An array is given to find the maximum continuous and.

Idea: Here we use the Division method to solve. As with other partition on the array, we divide the array a into b,c;

Maximum continuous and there are three possibilities: 1. In array B. 2. In array C. 3. Cross the midpoint of array A, part B, and part C.

For the first two cases, we can recursively handle it, so we need to consider the third case.

One thing to note is that this is a continuous and cross-midpoint, so we can derive the maximum continuity from the left and right, respectively, and then synthesize the two segments.

Note: Because of the requirements and, the initial value of the variable is smaller than the minimum.

#include <cstdio> #include <algorithm> #include <cstring> using namespace std;

const int MAX = 100010;
    struct aa{int s,l,r; AA (int _s, int _l, int _r): s (_s), L (_l), R (_r) {} bool operator > (const AA & RHS) const{return s > RH S.S | |
    (s = = RHS.S && l < RHS.L);

}
};

int T,n,a[max];
    AA Solve (int left, int. right) {if (left = right) return AA (A[left],left,right);
    int mid = (left + right) >> 1;
    AA res = solve (Left,mid);
    AA tmp = Solve (mid+1,right);

    if (tmp > Res) res = tmp;
    int lsum = -2000, rsum = -2000,tsum = 0,lpos,rpos;
        for (int i = Mid, I >= left; i) {tsum + = A[i];
            if (Tsum > Lsum | | tsum = = lsum) {lsum = Tsum;
        Lpos = i;
    }} tsum = 0;
        for (int i = mid + 1, I <= right; ++i) {tsum + = A[i];
            if (Tsum > rsum) {rsum = Tsum;
        RpoS = i; }} tmp = AA (lsum+rsum,lPos,rpos);
    if (tmp > Res) res = tmp;
return res;
    } int main (int argc,char * argv[]) {//freopen ("Input.txt", "R", stdin);
    scanf ("%d", &t);
       for (int i = 1; I <= T; ++i) {scanf ("%d", &n);
       for (int j = 0; j < N; ++j) scanf ("%d", &a[j]);
       AA res = solve (0,n-1);
       if (i! = 1) puts ("");
    printf ("Case%d:\n%d%d%d\n", i,res.s,res.l+1,res.r+1);
} return 0;
 }


Related Article

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.