当前位置: 代码迷 >> 综合 >> HDU 1231 最大连续子序列 DP .
  详细解决方案

HDU 1231 最大连续子序列 DP .

热度:93   发布时间:2023-09-23 06:54:05.0

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1231

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=10000+5;
const int INF=0x3f3f3f3f;
int n[maxn],d[maxn];
int main()
{int N,L,R,sum;while(cin>>N,N){memset(d,0,sizeof(d));int cnt=0;for(int i=1;i<=N;i++){scanf("%d",&n[i]);cnt+=n[i]<0;}if(cnt==N) {putchar('0');cout<<' '<<n[1]<<' '<<n[N]<<endl;continue;}for(int i=1;i<=N;i++)d[i]=max(d[i-1]+n[i],n[i]);R=max_element(d+1,d+1+N)-d;for(L=R,sum=d[R]-n[R];sum&&L>=1;--L,sum-=n[L]);cout<<d[R]<<' '<<n[L]<<' '<<n[R]<<endl;}return 0;
}