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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值