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("\n");
        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:\n",qq++);
        printf("%d %d %d\n",Max,be+1,end+1);
    }
    return 0;
}
posted @ 2016-07-06 08:18  see_you_later  阅读(108)  评论(0编辑  收藏  举报