Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142281 Accepted Submission(s): 33104
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000). Output For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases. Sample Input 2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5 Sample Output Case 1: 14 1 4 Case 2: 7 1 6 問題的想法是《編程之美》上的,修改了一下提交就ok了。。 思路: 考慮數組的第一個元素a[0],以及最大的一段數組(a[i].....a[j])跟a[0]之間的關系,有一下幾種情況: 1. 當0=i=j時,元素a[0]本身構成和最大的一段。 2.當0=i<j時,和最大的一段以a[0]開始。 3.當0<i時,元素a[0]跟和最大的一段沒有關系。 假設已經知道(a[1]....a[n-1])中的最大的一段數組之和為All[1],並且已經知道了(a[1]......a[n-1])中包含a[1]的和最大的一段數組為start[1]。 那麼有以上情況可以得出(a[0].....a[n-1])中問題的解All[0]是三種情況的最大值max(a[0],a[0]+start[1],All[1]). so問題符合無後效性,用動態規劃方法可解決。1 #include<iostream> 2 using namespace std; 3 int main() 4 { 5 int t,len,m=0,h,n,w,l,r,p,e,i; 6 cin>>t;h=t; 7 while(t--) 8 { 9 m++; 10 l=r=p=e=0; 11 cin>>len; 12 int a[len]; 13 for(i=0;i<len;i++) 14 cin>>a[i]; 15 n=a[0],w=a[0];l=0;p=0; 16 for(i=1;i<len;i++) 17 { 18 if(a[i]>n+a[i]){ 19 n=a[i]; 20 l=i; 21 r=i; 22 } 23 else { 24 n=n+a[i]; 25 r=i; 26 } 27 if(n>w){ 28 w=n; 29 e=r; 30 p=l; 31 } 32 } 33 cout<<"Case "<<m<<":"<<endl<<w<<" "<<p+1<<" "<<e+1<<endl; 34 if(m!=h)cout<<endl; 35 } 36 return 0; 37 }
#include<stdio.h>
#include<stdlib.h>
int a[100005],sum[100005];
int main()
{
int ca,count=0;
scanf("%d",&ca);
while(ca--)
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sum[1]=a[1];
int r=1,max=a[1];
for(i=2;i<=n;i++)
{
if(sum[i-1]>0)
{
sum[i]=sum[i-1]+a[i];
if(sum[i]>max)
{
max=sum[i];
r=i;
}
}
else
{
sum[i]=a[i];
if(sum[i]>max)
{
max=sum[i];
r=i;
}
}
}
count++;
for(i=r-1;i>0;i--)
if(sum[i]<0) break;
printf("Case %d:\n",count);
printf("%d %d %d\n",max,i+1,r);
if(ca!=0) printf("\n");
}
}
#include <iostream>
using namespace std;
__int64 f[100005]; // 放在main外防止Stack Overflow
int i,j,n,m,l,k=0,a[100005];
int main()
{
__int64 max;
scanf("%d",&n); // 讀入n
while(++k)
{
if(k>n)break;
scanf("%d",&m);
memset(f,0,sizeof(f));//初始化數組
memset(a,0,sizeof(a));
max=-1001;
for (i=1;i<m+1;i++)
scanf("%d",&a[i]);//輸入數組
f[1]=a[1];
for(i=2;i<=m;i++)//求第i個數之前的最大和
{
if(f[i-1]>0)
f[i]=a[i]+f[i-1];
else f[i]=a[i];
}
for(i=1;i<m+1;i++)
if(max<f[i])
{
max=f[i];
l=i;
}
for(i=l;i>=1;i--)
if(f[i]<0)
break;
if(max>=0) i++; // 全部是負數的時候需要+1
printf("Case %d:\n",k); // C大寫
printf("%I64d %d %d\n",max,i,l);
if(k<n) printf("\n"); // 中間的空行
}
}