Max Sum

本来用DP暴力枚举的,竟然没过……老尴尬了

然后想是不是得哦o(nlog n)或者o(n)

感觉没什么要讲的,代码简单

 

#include <iostream>
#include<cstdio>
using namespace std;

int main()
{
int good[3],pre[100001];
int t;
cin>>t;
for(int k=1;k<=t;k++){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&pre[i]);
}
good[0]=pre[1],good[1]=1,good[2]=1;
int temp=0,tpos=1;
for(int i=1;i<=n;i++){
temp+=pre[i];
if(temp>good[0]){
good[2]=i;
good[1]=tpos;
good[0]=temp;

}
if(temp<0){
temp=0;
tpos=i+1;
}
}
printf("Case %d:\n%d %d %d\n",k,good[0],good[1],good[2]);
if(k!=t)
printf("\n");
}
return 0;
}

posted @ 2020-02-17 08:27  sos3210  阅读(127)  评论(0编辑  收藏  举报