Problem A
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 254 Accepted Submission(s) : 47
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Case 1: 14 1 4 Case 2: 7 1 6
这道题主要的问题是求出总序列的最大和,而每个数都有加到前面作为前面已经加好的和的增量和自己独立成为一个“最大和”的选择,在这两个选择中的最大和就是局部的最大和,而保存好第一个最大和,将整个序列的所有局部最大和都求解出来,就能得到全列的最大和
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
int main(){
int ca=1,t,s,e,n,x,now,before,max;
cin>>t;
while(t--) {
cin>>n;
for(int i=1;i<=n;i++) {
cin>>now;
if(i==1){
max=before=now;
x=1;
s=1;
e=1;
}
else{
if(now>now+before)
{
before=now;
x=i;
}
else
before+=now;
}
if(before>max)
max=before,s=x,e=i;
}
cout<<"Case"<<ca++<<":"<<endl;
cout<<max<<" "<<s<<" "<<e<<endl;
if(t)
cout<<endl;
}
return 0;
}