1003 Max Sum(动态规划)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 137169    Accepted Submission(s): 31787

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
 
Sample Output
Case 1:
14 1 4
 
Case 2:
7 1 6
 
第一次测试,未通过,显示是“Time Limit Exceeded”,程序如下。
 
#include<stdio.h>
int main()
{
    int T,i,j,t,sum=0,k,p,m,max,start,end;
    scanf("%d",&T);//T为Case数
    
    for(i=1;i<=T;i++)
    {
        int s[100000];
        scanf("%d",&s[0]);
        
        for(j=1;j<=s[0];j++)
         {
              scanf("%d",&s[j]);
        }
        max=s[1];start=1;end=1;
        for(t=1;t<=s[0];t++)//t代表每次连续相加的整数个数 
            for(k=1;k<=s[0]-t+1;k++)//k代表每次相加时start位置 
            {
                p=k; 
                for(m=1;m<=t;m++)//m用作每次相加时的计数,直到m=t时,停止 ,此处for循环一次,得到一个sum 
                {
                    sum+=s[p];
                    p++;
                }
                if(max<sum)
                 {
                     max=sum;
                    start=k;
                    end=k+t-1;
                }
                sum=0;
            }
        printf("Case %d :
",i);
        printf("%d %d %d

",max,start,end);        
            
    }
    return 0;
}
View Code
 
经过查询,本题属于初级的动态规划(DP)问题,从一串数字选出和最大的子串。我前边的做法是将所有可能的组合都列出来,再从中选出最大的那一个。虽然也能得出正确结果,但是使用了嵌套三次的for循环,使得本次程序的时间复杂度过大,O(n^3),程序最后运行时间超过要求。
 

题解:

以下是变量说明:
t	        测试数据组数
n	        每组数据的长度
temp	        当前取的数据
pos1  	最后MAX SUM的起始位置
pos2	        最后MAX SUM的结束位置
max		当前得到的MAX SUM
now		在读入数据时,能够达到的最大和
x		记录最大和的起始位置,因为不知道跟之前的max值的大小比,所以先存起来

下面模拟过程:
1.首先,读取第一个数据,令now和max等于第一个数据,初始化pos1,pos2,x位置
2.然后,读入第二个数据,判断
①. 若是now+temp<temp,表示当前读入的数据比之前存储的加上当前的还大,说明可以在当前另外开始记录,更新now=temp
②. 反之,则表示之前的数据和在增大,更新now=now+temp
3.之后,把now跟max做比较,更新或者不更新max的值,记录起始、末了位置
4.循环2~3步骤,直至读取数据完毕。
具体程序如下:
#include<stdio.h>
int main()
{
    int T,i,j,t,num,temp,sum,max,pos1,pos2,now,x;
    scanf("%d",&T);//T为Case数
    
    for(i=1;i<=T;i++)
    {
        scanf("%d",&num);
        scanf("%d",&temp);
        max=temp;now=temp;
        pos1=1;pos2=1;x=1;//x用来标注now的开始位置 
        for(j=2;j<=num;j++)
        {
            scanf("%d",&temp);
            if(now+temp<temp)
            {
                now=temp;
                x=j;
            }
            else
                now+=temp;
            if(now>max)
            {
                max=now;
                pos1=x;
                pos2=j;
            }
                
        }
        printf("Case %d:
",i);
        printf("%d %d %d
",max,pos1,pos2);
        if(i!=T)
            printf("
");    
            
    }
    return 0;
}
View Code

参考链接:1,http://blog.csdn.net/akof1314/article/details/4757021
     2,http://blog.163.com/tanliwu90@126/blog/static/250961612009111115639619/

 
原文地址:https://www.cnblogs.com/omigia/p/3746469.html