1.题目编号:1001
2.简单题意:给一个序列a[1],a[2]...a[n],计算该序列的最长子序列的最大值,但是如果有超过一个子序列的,选择第一个子序列。
3.解题思路形成过程:看到这个题,就是用动态规划求最大子序列,开始用的方法一直超时,嵩哥说我写的太麻烦,应该用老师上课讲的,输入的n个数相加放在一个sum数组里面,如果sum[i-1]+a[i]的值为负的就将sum[i]=a[i],否则与a[i]相加之后放入sum[i]中,另外这个题需要记录字段的开始位置和结束位置,可以用个tag数组将是一个字段的第一个位置放在tag中,而tag的下标可以知道结束位置。
4.感悟:哈哈,人多力量大啊,提交了不下10遍的程序终于AC了,多谢嵩哥的指导,虽然过程非常坎坷,但是最终AC了,感觉自己萌萌哒~其中我把记录开始位置的放在了循环里面,结果就出错了,如5 1 2 -4 3 2初始位置还为1 还忘了q在再循环的时候应该置1,否则前面是后面的数max为第一个数但是前一个循环的结束位置不为1,则这次的就错误。
5.AC的代码:
#include<iostream>
#include<string.h>
using namespace std;
int main(){
//freopen("1.txt","r",stdin);
int T,n,q,a[100002]={0},tag[100003],t,sum[100003]={0},max,c=0;
cin>>T;
while(T--){
q=1;
cin>>n;
t=1;
for (int i=1;i<=n;i++){
cin>>a[i];
if(sum[i-1]>=0){
sum[i]=sum[i-1]+a[i];
tag[i]=t;
}
else{
sum[i]=a[i];
tag[i]=i;
t=0+i;
}
}
max=sum[1];
for (int j=2;j<=n;j++){
if (max<sum[j]){
max=sum[j];
q=j;
}
}
c++;
cout<<"Case "<<c<<":"<<endl;
cout<<max<<" "<<tag[q]<<" "<<q<<endl;
if (T!=0){
cout<<endl;
}
}
return 0;
}
原题:
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.<br>
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).<br>
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.<br>
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