Algorithm for Job third experiment

/ * Maximum field and 105 (15 min)
C Time Limit: 3000 ms | C memory limit: 3000 Kb
Title SUMMARY:
given a sequence of length n, an integer, a [1 ... n], seeking [1, n ] of a sub-interval [i, j] such that a [i] + ... + a [j] and the maximum.
, or find the maximum and this example (-2,11, -4,13, 5,2) of and the maximum of 20 sub-segment, the Qiuzi interval [2,4].
enter a description of
the first acts integer n, there are n represents an array of data, the second row are sequentially input n integers
output descriptor
calculated n integers k and the maximum number of consecutive
input samples
. 5
. 1. 4. 5 2. 3

. 7
. 6. 8. 5 -5 -13. 5. 7

the output sample
15
14
* /

#include<iostream>
#include<stdio.h>
using namespace std;
const int maxn = 1e4+10;
int a[maxn];
int dp[maxn][maxn];
int main(){
    int n;
    cin>>n;
    for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
    
    for(int i=0;i<n;i++){
        for(int j=i;j<n;j++){
            if(j==i)
                dp[i][j] = a[i];
            else
                dp[i][j] =dp[i][j-1]+a[j];
        }
    }
    int m=0;
    for(int i=0;i<n;i++){
        for(int j=i;j<n;j++){
            m = max(m,dp[i][j]);
        }
    }
    cout<<m<<endl;

    return 0;
} 

 

Guess you like

Origin www.cnblogs.com/lusiqi/p/11736601.html