1003-Max Sum
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
解题思路:
解这道题用的是动态规划,但要考虑的情况却是非常的多,第一重要的也是最容易忽视的就是要考虑当所有的数据都是负数时,这种情况要特殊考虑,第二就是在记录最大和的区间的时候,计算没个f[i]的时候都要将他的区间更新一次。做这题的时候,经过了无数次蛋疼的WA,下面是AC的代码,因为一开始就没写好,很多情况都是后来加上去的,所以代码显得有点繁琐。
AC代码:
 #include<iostream>
#include<stdio.h>
using namespace std;
int T,a[1000010];
struct fun {
int date;
int front,rear;
}f[1000010];
int ma(int x,int y) {
return (x>y? x:y);
}
int main() {
scanf("%d",&T);
for(int l=1;l<=T;++l) {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
int k=0;
for(int i=1;i<=n;++i)
if(a[i]>0)
k=1;
int max=1;
if(k){
for(int i=0;i<n;++i)
f[i].front=1;
f[0].date=0;
int biaoji=1;
for(int i=1;i<=n;++i) {
if(f[i-1].date+a[i]<0 )
biaoji=i+1;
else
f[i].front=biaoji;
f[i].rear=i;
f[i].date=ma(0,f[i-1].date+a[i]);
}
for(int i=2;i<=n;++i)
if(f[i].date>f[max].date) {
max=i;
f[max].rear=max;
}
}
else if(k==0) {
int min=1;
for(int i=2;i<=n;++i)
if(a[i]>a[min])
min=i;
max=min;
f[max].date=a[min];
f[max].front=min;
f[max].rear=min;
}
printf("Case %d:\n%d %d %d\n",l,f[max].date,f[max].front,f[max].rear);
if(l!=T)
printf("\n");
}
return 0;
}
    
#include<stdio.h>
using namespace std;
int T,a[1000010];
struct fun {
int date;
int front,rear;
}f[1000010];
int ma(int x,int y) {
return (x>y? x:y);
}
int main() {
scanf("%d",&T);
for(int l=1;l<=T;++l) {
int n;
scanf("%d",&n);
for(int i=1;i<=n;++i)
scanf("%d",&a[i]);
int k=0;
for(int i=1;i<=n;++i)
if(a[i]>0)
k=1;
int max=1;
if(k){
for(int i=0;i<n;++i)
f[i].front=1;
f[0].date=0;
int biaoji=1;
for(int i=1;i<=n;++i) {
if(f[i-1].date+a[i]<0 )
biaoji=i+1;
else
f[i].front=biaoji;
f[i].rear=i;
f[i].date=ma(0,f[i-1].date+a[i]);
}
for(int i=2;i<=n;++i)
if(f[i].date>f[max].date) {
max=i;
f[max].rear=max;
}
}
else if(k==0) {
int min=1;
for(int i=2;i<=n;++i)
if(a[i]>a[min])
min=i;
max=min;
f[max].date=a[min];
f[max].front=min;
f[max].rear=min;
}
printf("Case %d:\n%d %d %d\n",l,f[max].date,f[max].front,f[max].rear);
if(l!=T)
printf("\n");
}
return 0;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号