Maximum continuous sum (maxsum)
Time Limit: 1 s
Space: 256 m
Description:
Given a series of N integers, You must select a segment with a length not greater than m to maximize the sum of these segments.
Input Format:
The first line contains two integers, N, and M, indicating the length of the sequence and the limit on the maximum continuous length and length.
N numbers in the next row describe this series
Output Format:
Output a number to indicate the maximum continuous sum of the limit width.
Data scope and conventions:
30% n, m <= 10
50% n, m <= 100
70% n, m <= 1000
100% 1 <= n, m <= 10 ^ 5
Example:
Input
4
2-1-3 4
Output
4
Input
4 3
2 2 3 4
Output
9
Prefix and
I ~ Sum [I]-sum [J-1] between J
And then enumerate each vertex I
Because J's range is known
To make sum [I]-sum [J-1] the sum and the maximum since sum [I] is determined, only one minimum value is selected within the range of J.
Therefore, you can use a monotonous queue or line segment tree to maintain and find the minimum interval.
The first 70 points of code
1 # include<cstdio> 2 # include<algorithm> 3 using namespace std; 4 const int maxn=1005; 5 int f[maxn][maxn],num[maxn]; 6 int Max,n,m; 7 void solve_1000(){ 8 Max=-999999999; 9 int t=1;10 for(int i=1;i<=n;i++)11 for(int j=t++;j<=n;j++)12 f[i][j]=f[i][j-1]+num[j];13 t=1;14 /* for(int i=1,tt=0;i<=n;i++,tt=0){15 for(int j=t++;j<=n;j++)16 if(tt++<m)printf("f[%d][%d]=%d ",i,j,f[i][j]);17 printf("\n");18 }*/19 for(int i=1,tt=0;i<=n;i++,tt=0)20 for(int j=t++;j<=n;j++)21 if(tt++<m)Max=max(Max,f[i][j]);22 printf("%d",Max);23 }24 void init(){25 scanf("%d%d",&n,&m);26 for(int i=1;i<=n;i++)27 scanf("%d",&num[i]);28 if(n<=1000)solve_1000();29 else solve30 }31 int main(){32 freopen("maxsum.in","r",stdin);33 freopen("maxsum.out","w",stdout);34 init();35 return 0;36 }View code
[Simulation competition] maximum continuous limit and (maxsum)