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;
}