HDOJ1003

MAX —sum  好久前做过 但是再看到的时候觉得的有点陌生

设Si是一定以i结尾的最大连续子序列

S1=a[1];

Sn=Sn-1>=0?Sn-1+a[n]:a[n];//状态转移大致是这样 则S1 —Sn中必有所求子序列 边存边比较 答案就出来了

#include<stdio.h>
int main()
{
    int a,P,n,start,temp,max,end,sum,mark=0;
    scanf("%d",&P);
        while(P--)
        {
            max=-1000,temp=1,sum =0;
            scanf("%d",&n);
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&a);
                sum+=a;
                if(sum>max)
                {
                    max=sum;
                    start=temp;
                    end=i;
                }
                if(sum<0)
                {
                    sum=0;
                    temp=i+1;
                }
            }
            printf("Case %d:\n%d %d %d\n",++mark,max,start,end);
            if(P!=0){
                printf("\n");
            }
        }
    return 0;
}
时间: 2024-08-11 01:35:06

HDOJ1003的相关文章

HDOJ-1003 Max Sum(最大连续子段 动态规划)

http://acm.hdu.edu.cn/showproblem.php?pid=1003 给出一个包含n个数字的序列{a1,a2,..,ai,..,an},-1000<=ai<=1000 求最大连续子段和及其起始位置和终止位置,很基础的动态规划(DP)问题,看完DP第一次做的DP题目 DP真的是一种很优美的算法,或者说思想,但是比较难理解,我对DP的理解还很浅薄 # include <stdio.h> # define INF 1000000000 int main() { i

SDUT 1351 Max Sum

Max Sum Time Limit: 2000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 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 =

HDU--Max sum---DP练习

Max Sum Time Limit: 2000ms   Memory limit: 32768K  有疑问?点这里^_^ 题目描述 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 =