杭电1003 ,好歹对了,但是空间复杂度挺高的。。。。。

/*这个算法是以前在编程之美上看过的,正好遇到了,做一下*/
#include
#include
#include
using namespace std;
int main(){
int n;
cin>>n;
int **a=new int* [n];
int **sum=new int *[n];//储存带有a[i]的最大值
int **maxsum=new int *[n];//储存全局的最大值
int p[20];
memset(p,0,sizeof(p));
for(int i=0;i cin>>p[i];
a[i]=new int[p[i]];
sum[i]=new int[p[i]];
maxsum[i]=new int [p[i]];
for(int j=0;j cin>>a[i][j];
}
}
int *maxpos=new int[n];
int *minpos=new int[n];
for(int i=0;i maxsum[i][p[i]-1]=a[i][p[i]-1];
sum[i][p[i]-1]=a[i][p[i]-1];
maxpos[i]=p[i];
minpos[i]=p[i];
int temp1=maxpos[i];
     int temp2=minpos[i];
for(int j=p[i]-2;j>=0;--j){
if(a[i][j]>=a[i][j]+sum[i][j+1]){
sum[i][j]=a[i][j];
temp1=j+1;
temp2=j+1;
}
else
{
sum[i][j]=a[i][j]+sum[i][j+1];
temp2=j+1;
}
maxsum[i][j]=max(sum[i][j],maxsum[i][j+1]);
if(sum[i][j]>=maxsum[i][j+1]){
maxsum[i][j]=sum[i][j];
minpos[i]=temp2;
maxpos[i]=temp1;
}
else {
maxsum[i][j]=maxsum[i][j+1];
}

}

}
for(int i=0;i   cout<<"Case "< if(i cout< }
}
for(int i=0;i delete a[i];
a[i]=NULL;
}
for(int i=0;i delete maxsum[i];
maxsum[i]=NULL;
}
for(int i=0;i delete sum[i];
sum[i]=NULL;
}
//system("pause");
//delete [][]a;
//delete [][]sum;
return 0;
}

你可能感兴趣的:(没事写小程序)