周三,还是道求最大子列和的问题,一个字母。。。跪了
HDU 1003 Max Sum
Given a sequence a1,a2,a3……an
, 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
是时候给最大子列和的问题完结一下了;这是一类动态规划的经典题目,但是我却喜欢用另外一种方法,因为这类问题很特殊,如果不要求最大子列的起始位置与结束的位置,那么有一种生气的方法叫做在线处理(代码如下)
这个代码复杂度是o(n),接近是一个完美的算法了,只要遍历一遍就可以求出答案。
那么还可以用分治的做法,求出将问题分解成小规模问题后,求出左边的最大子列,再求出右边的最大子列,最后求出跨分界线的最大子列,求三个中的max即可;代码如下:
#include <iostream>
#include <cstdlib>
using namespace std;
int List[10050];
int l[10], r[10];
int max_num(int a, int b, int c){//求三个数最大值
int Max;
if(a>b)Max=a;
else Max=b;
if(Max>c)return Max;
else return c;
}
int divide(int List[], int l, int r){
int Lsum, Rsum;
int Leftpart, Rightpart;
int Lmaxsum,Rmaxsum;
int mid, i;
if(l==r){
if(List[l]>0)return List[l];
else return 0;
}
mid=(r-l)/2+l;
Lsum=divide(List,l,mid);
Rsum=divide(List,mid+1,r);
Lmaxsum=0;Leftpart=0;
for(i=mid;i>=l;i--){
Leftpart+=List[i];
if(Leftpart>Lmaxsum)Lmaxsum=Leftpart;
}
Rmaxsum=0;Rightpart=0;
for(i=mid+1;i<=r;i++){
Rightpart+=List[i];
if(Rightpart>Rmaxsum)Rmaxsum=Rightpart;
}
return max_num(Lsum,Rsum,Lmaxsum+Rmaxsum);
}
int max_list(int List[], int n){
return divide(List, 0, n-1);
}
int main(){
int n;
while(cin>>n&&n){
int i, j=0, tmp=0, maxx=-1, st=0, ed=n-1;
for(i=0;i<n;i++)
cin>>List[i];
for(i=0;i<n;i++){
tmp+=List[i];
if(tmp>maxx){
st=j;ed=i;maxx=tmp;
}
else if(tmp<0){
j=i+1;
tmp=0;
}
}
cout<<max_list(List, n)<<" "<<List[st]<<" "<<List[ed]<<endl;
}
return 0;
}
动态规划的想法也很好,状态转移方程很简单 如下,附代码:
if((dp[i-1]+a[i])>=a[i]){
dp[i]=dp[i-1]+a[i];
st[i]=st[i-1];
last[i]=i;
}else {
dp[i]=a[i];
st[i]=last[i]=i;
}
#include <iostream>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int main()
{
int k,res,i,t=0,n,cnt=1;
cin>>n;
while(n--){
cin>>k;
int a[123456]={0},dp[123456]={0},st[123456]={0},last[123456]={0};
res=-inf;
for(i=0;i<k;i++){
cin>>a[i];
}
dp[0]=a[0];
st[0]=last[0]=0;
for(i=1;i<k;i++){
if((dp[i-1]+a[i])>=a[i]){
dp[i]=dp[i-1]+a[i];
st[i]=st[i-1];
last[i]=i;
}else {
dp[i]=a[i];
st[i]=last[i]=i;
}
}
for(i=0;i<k;i++){
if(res<dp[i]){
t=i;
res=dp[i];
}
}
cout<<"Case "<<cnt<<":"<<endl;
cout<<res<<" "<<st[t]+1<<" "<<last[t]+1<<endl;
if(n)
cout<<endl;
cnt++;
}
return 0;
}
我最喜欢的方法,注意点是maxx赋值可以给一个很小的值inf=-0x3f3f3f3f,或者是数据的下限减一;然后数据初始化要放对位置,放在while(n–){…}里面,遇到temp<0时,原来的尾巴下标+1就是新的头,即j=i+1;
tips:数组第一位a[0]可以不用,直接i=1起步;代码如下:
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100010];
int main(){
int t,p;
cin>>t;
p=t;
while(t--){
int n,i;
cin>>n;
int st=1,ed=1,j=1,temp=0,maxx=-1001;
for(i=1;i<=n;i++)
cin>>a[i];
for(i=1;i<=n;i++){
temp+=a[i];
if(temp>maxx){
maxx=temp;st=j;ed=i;
}
if(temp<0){
temp=0;j=i+1;
}
}
cout<<"Case "<<p-t<<":"<<endl;
cout<<maxx<<" "<<st<<" "<<ed<<endl;
if(t)cout<<endl;
}
return 0;
}
哈哈哈,今天到此结束,上铂金啦,困高困高。。