Personal experience: This is a good question, the maximum linear field and in the dynamic planning is commonplace. So a clever shift in the idea of such dynamic planning also requires
Exercise, like in the summer camp in the encounter from 1 to K is incremented, K to N is the decrement of K makes at this time and the largest, at that time is also helpless, although the back
Think of the difference will be expanded and then separately to increase the maximum and, the problem is solved. In fact, the decomposition of the problem is still very clear,
The maximum value is nothing more than the linear dynamic programming and the sub-segments across the two ends, the simplest of the sub-segments across the two layers is the two-layer loop is obviously timed out, the back of the left to find the largest
And signs, but it is obvious that the maximum value of the two segments is not necessarily in the premise of a maximum value, so after listening to the views of teammates, found out the middle of the largest negative length of the sub-segment and.
Hope to be able to develop more ideas in the future, so that the problem becomes more simple!
Topic:
n integers consisting of a cyclic sequence a[1],a[2],a[3],..., a[n], the maximum number of consecutive sub-segments of the sequence such as A[i]+a[i+1]+...+a[j] (the loop sequence refers to the number of n is enclosed in a circle, so the need to consider a[n-1],a[n],a[1],a[2] Such a sequence). When the given integer is a negative number and is 0. For example: -2,11,-4,13,-5,-2, and the largest subkey: 11,-4,13. and for 20. Input
Line 1th: Length of the integer sequence N (2 <= N <= 50000) line 2-n+1: n integers ( -10^9 <= s[i] <= 10^9)
Output
The maximum number of sub-segments and of the output loop array.
Input example
6-211-413-5-2
Output example
20
1#include <iostream>2#include <cstring>3#include <string>4#include <cstdio>5#include <vector>6#include <cmath>7#include <stack>8#include <Set>9#include <queue>Ten#include <algorithm> One using namespacestd; A #defineIn 1000000007 - intMain () - { the intN; -Cin>>N; - intnumber[50005]; - Long Longsum=0; + for(intI=0; i<n;i++) -Cin>>Number[i]; + Long LongMaxa=0; A Long Longt=0; at Long Longp=0, q=0; - for(intI=0; i<n;i++) - { -sum+=Number[i]; -t+=Number[i]; -p+=Number[i]; in if(t>0) t=0; - if(p<0) p=0; to if(P>Q) q=p; + if(maxa>t) maxa=T; - } theCout<<max (SUM-MAXA,Q) <<Endl; * return 0; $}
Maximal sub-segments of Loop array and (clever transformation of dynamic programming ideas)