hdu1231最大子序列

 http://acm.hdu.edu.cn/showproblem.php?pid=1231 

dp的题目大部分只要找出状态方程就很好做,本题是

 if (current<0)
{
   current=a[i];
temp=a[i];
}
else 
current+=a[i];
if (current>res)
{
   res=current;
first=temp;
last=a[i];
}

current是当前的要判断的值,当他是负数时,总体更新头尾节点,刷新最大值,从头dp就能解决有多个最大子序列的问题,且判断是不能是大于等于。下面是源代码

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
#define Max 10001
int max(int a,int b) {if (a>b) return a; else return b;}
int first,last,n,res;
int a[Max];
int main()
{
int i,temp;
for (;;)
{
   cin>>n;
memset(a,0,sizeof(a));
if (!n) {break;}
else 
{
   for (i=0;i<n;i++)
cin>>a[i];
}
res=a[0];
first=a[0];
last=a[0];
int current=0;
for (i=0;i<n;i++)
{
   if (current<0)
{
   current=a[i];
temp=a[i];
}
else 
current+=a[i];
if (current>res)
{
   res=current;
first=temp;
last=a[i];
}
}
if (res<0) {printf("0 %d %d\n",a[0],a[n-1]);}  
else {printf("%d %d %d\n",res,first,last);}
}
    return 0;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值