hdu 1003 hdu 1231 最大连续子序列【dp】

时间:2023-12-17 08:51:14

HDU1003 HDU1231

题意自明。可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了。

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+;
int a[MAXN]; int main()
{
int T,N;
scanf("%d",&T);
for(int cas=;cas<=T;cas++)
{
scanf("%d",&N);
for(int i=;i<=N;i++){
scanf("%d",&a[i]);
}
int ans=-1e9;
int l=,r=,pos=;
int tot=;
for(int i=;i<=N;i++){
tot+=a[i];
if(tot>ans){
ans=tot;
r=i;
l=pos;
}
if(tot<){
tot=;
pos=i+;
}
}
printf("Case %d:\n%d %d %d\n",cas,ans,l,r);
if(cas!=T)
printf("\n");
}
return ;
}

HDU 1003

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1e5+;
int a[MAXN];
int N; int main()
{
while(scanf("%d",&N)==,N)
{
int cnt=;
for(int i=;i<=N;i++){
scanf("%d",&a[i]);
if(a[i]<) cnt++;
}
int ans=-1e9;
int l=,r=,pos=;
int tot=;
for(int i=;i<=N;i++){
tot+=a[i];
if(tot>ans){
ans=tot;
r=i;
l=pos;
}
if(tot<){
tot=;
pos=i+;
}
}
if(cnt==N){
ans=;
printf("%d %d %d\n",ans,a[],a[N]);
}else{
printf("%d %d %d\n",ans,a[l],a[r]);
}
}
return ;
}

HDU 1231