zoukankan      html  css  js  c++  java
  • hdoj1003【DP】

    这道题目的DP,写到现在才明白。。。
    每次加或者不加的条件就是这个前面这个子序列合是不是大于等于0,如果不是加了就会让这个位置的值没有意义,如果大于0,他还是在往递增的方向继续前进。
    以及这个条件的继续理解:我想用三个例子
    )1:5 -1 -2 -3
    )2:5 1 -2
    )3:5 -1 -2 1 1
    可以试着模拟一下,为什么这个判断条件的正确性

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stack>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <math.h>
    #include <algorithm>
    using namespace std;
    #define LL long long
    #define INF 0x3f3f3f3f
    const double pi = acos(-1.0);
    
    const int mod =9973;
    
    const int N = 1e5+10;
    
    int a[N];
    
    int main()
    {
        int qq=1;
        int T,n;
        cin>>T;
        while(T--)
        {
            if(qq>1)
                printf("
    ");
            scanf("%d",&n);
            for(int i=0;i<n;i++)
            {
                scanf("%d",&a[i]);
            }
            int Max,tep;
            int pt,end,be;
            be=end=pt=0;
            Max=tep=a[0];
            for(int i=1;i<n;i++)
            {
                if(tep>=0)
                {
                    tep+=a[i];
                }
                else
                {
                    tep=a[i];
                    pt=i;
                }
                if(Max<tep)
                {
                    Max=tep;
                    be=pt;
                    end=i;
                }
            }
            printf("Case %d:
    ",qq++);
            printf("%d %d %d
    ",Max,be+1,end+1);
        }
        return 0;
    }
  • 相关阅读:
    第十周学习进度条
    第九周学习进度条
    Runner站立会议08
    Runner站立会议07
    构建之法阅读笔记02
    Runner站立会议04
    学习进度条
    Runner站立会议01
    进度条
    返回一个一维整数数组中最大子数组的和
  • 原文地址:https://www.cnblogs.com/keyboarder-zsq/p/5934481.html
Copyright © 2011-2022 走看看