• DP杭电1003 MAX SUM


    Max Sum

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


    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
     
     
     
    这是一道DP。
        一开始的时候在想怎么把它变成一个个的子问题然后储存起来,当时走了一个弯路就是想把每个i内的最大和子串然后进行比较,然后通过判断最后一个位置是不是在i上。后来觉得比较难实现。就放弃了;
        再后来看了下解题报告,但是没大看懂。觉得最大和字串的最后位置一点在1~n的某个i上,只要是确定以i结尾的每个最大和子串的值及其起点就可以了;
        这里建立了一个结构体。f[i]来表示每个以i结尾的最大和字串的值。
    原理:
        if都是正数   自然len = n是最大的
        else if 都小于0 自然是绝对值最小的那个值是最大的(和第中可以归结到一起)
        if 有正有负 
    {
        因为我们求的f[i]是每个以i结尾的字串中的最大值,所以必须让l(end) = i。所以只要是前面的f[i-1]是以i-1结尾的。那么f[i]的值就只有两种 1 a[i] 2 f[i-1] + a[i]
        所以只要只要求 f[i-1] + a[i] > a[i]即可;
      这样就有了状态转换方程 f[i] = 取最大(f[i-1] + a[i],a[i]);
      此方程对 情况1 2 同样适用。
      最后对f[i]中的sum进行比较就行了。
      这样就把另一个帖子中的穷举法的很多步骤的子问题的结果给储存起来,从而达到打表备忘法的目的。
    }
    一下是我自己的代码,有个地方我感觉对,但是不该就WA.我觉得这才应该是典型的DP做法。下面那个是我们头儿做的。但是说实话觉得不大好理解。
     
     
    View Code
    #include<stdio.h>
    struct node
    {
        int sum;
        int l;
        int r;
    }f[100000];
    int a[100000];
    int main()
    {
        
        int t,n;
    
        scanf("%d",&t);
        int count = 0;
        while(t--)
        {
        scanf("%d",&n);
    
        int i,j;
    
        for(i =  1;i <= n;i++)
        {
            scanf("%d",&a[i]);
        }
    
        f[1].sum = a[1];
        f[1].l = 1;
        f[1].r = 1;
        for(i = 2;i <= n;i++)
        {
            if(f[i-1].sum + a[i] >= a[i])//转换状态后的方程
            {
                f[i].sum = a[i] + f[i-1].sum;
                f[i].l= f[i-1].l;
                f[i].r = i;
            }
            else
            {
                f[i].sum = a[i];
                f[i].l = i;
                f[i].r = i;
            }
        }
    
        int temp;//不知道为什么不能将它变为一个结构体使他等于f[1]进行比较,那样wa了好几次,而且不能把temp变成一个sum为-1001的一个结构体,给她的l和rfu赋值为1;
        temp = 1;
        for(i =  1;i <= n;i++)
        {
            if(f[temp].sum < f[i].sum)
            {
                temp = i;
            }
        }
        count++;
        printf("Case %d:\n",count);
        printf("%d %d %d\n",f[temp].sum,f[temp].l,f[temp].r);
        if(t)
            puts("");
        }
        return 0;
    }

    头儿的代码。。

    View Code
    #include<stdio.h>
     int a[100001];
     int main()
     {
         long t,n,nowmax,premax,x,y,k = 0,i,start;
         scanf("%ld", &t);
         while(t--)
         {
             k++;
             scanf("%ld", &n);
             for(i = 1 ; i <= n ; i++)
                 scanf("%ld", &a[i]);          
             premax = -999;//因为输入数据大于-1000,premax比-1000大就行了
             nowmax = 0;
             start = 1;
             x = 1;
             y = 1;
             for(i = 1 ; i <= n ; i++)
             {
                 nowmax+=a[i];
                 if(nowmax>premax)
                 {
                     y = i;
                     start = x;//之前没有定义start 想着用下面的x就可以找出初始位置了 如果之前已经找到最大的premax 而后面又有负的nowmax 那x就不为初始位置 所以这里要跟着变动
                     premax = nowmax;
                 }           
                 if(nowmax<0)//这里本来写的else if 如果nowmax是负的并且比premax大 就不对了
                 {
                     x = i+1;
                     nowmax = 0;
                 }
             }
             printf("Case %ld:\n",k);
             printf("%ld %ld %d\n", premax,start,y);
             if(t!=0)
                 printf("\n");
         }
         return 0;
     }
  • 相关阅读:
    动态规划_leetcode416
    动态规划_leetcode377
    基础整理
    super使用简介
    PHP替换指定字符串
    yii安装redis扩展(Windows)
    PHP多维数组去重
    git pull
    vue页面部署并移除url里面的#号
    fatal: refusing to merge unrelated histories(git pull)
  • 原文地址:https://www.cnblogs.com/0803yijia/p/2450049.html
Copyright © 2020-2023  润新知