int maxsumblock(int a[],int n,int m,int cycle,int expand) { int i,j,k,tmp,totalmax=0,start; int sum[32][32]; int t[32]; for (i=0;i<n;i++) { for (j=0;j<m;j++) { printf("%d ",a[i*m+j]); if (i!=0) { sum[i][j]=sum[i-1][j]+a[i*m+j]; } else { sum[i][j]=a[i*m+j]; } } } for (i=0;i<n;i++) { for (j=0;j<=i;j++) { for (k=0;k<m;k++) { t[k] = (j==0)?0:-sum[j-1][k]; t[k] +=sum[i][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (tmp>totalmax) { totalmax= tmp; } if (expand) { for (k=0;k<m;k++) { t[k]=sum[n-1][k]-sum[i][k]+sum[j][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (tmp>totalmax) { totalmax= tmp; } } } } return totalmax; }
int main() { int m,n,j,i; int a[1024]; FILE * fin = fopen("input.txt","r"); fscanf(fin,"%d",&n); for (i=0;i<n;i++) { fscanf(fin,"%d",&a[i]); } printf("%d\n",maxsumcycle(a,n)); }
/* 求一个环的最大子序列 */ int maxsumblock(int a[],int n,int m,int cycle,int expand) { int i,j,k,tmp,totalmax=0; int sum[32][32]; int t[32]; X=n;Y=m; fout = fopen("internal_result.dat","w"); yy=0; for (i=0;i<n;i++) { for (j=0;j<m;j++) { if (i!=0) { sum[i][j]=sum[i-1][j]+a[i*m+j]; } else { sum[i][j]=a[i*m+j]; } } } for (i=0;i<n;i++) { for (j=0;j<=i;j++) { for (k=0;k<m;k++) { t[k] = (j==0)?0:-sum[j-1][k]; t[k] +=sum[i][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (maxsumline(t,m)>tmp) {ok =1;tmp=maxsumline(t,m);ok =0;} else {ok =1;tmp=maxsumcycle(t,m);ok =0;} if (tmp>totalmax) { totalmax= tmp; x1=j;x2=i; print_XY(); } if (expand) { for (k=0;k<m;k++) { t[k]=sum[n-1][k]-sum[i][k]+sum[j][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (tmp>totalmax) { totalmax= tmp; x1=i+1;x2=j; ok=2; print_XY(); ok=0; } } } } for (k=0;k<m;k++) { tmp=0; for (i=x1;;i=(i+1)%n) { tmp+=a[i*m+k]; if (i==x2) {break;} } t[k]=tmp; } tmp = maxsumline(t,m); if (cycle) { if (maxsumcycle(t,m)>tmp) { ok =1; tmp=maxsumcycle(t,m); ok =0; } else { ok = 1; maxsumline(t,m); ok =0; } } for (i=x1;;i=(i+1)%n) { for (j=yy;;j=(j+1)%m) { a[i*m+j]=-10001; if (j==y2) {break;} } if (i==x2) {break;} } fclose(fout); return totalmax; }
int maxsumblock(int a[],int n,int m,int cycle,int expand) { int i,j,k,tmp,totalmax=0,start; int sum[32][32]; int t[32]; for (i=0;i<n;i++) { for (j=0;j<m;j++) { if (i!=0) { sum[i][j]=sum[i-1][j]+a[i*m+j]; } else { sum[i][j]=a[i*m+j]; } } } for (i=0;i<n;i++) { for (j=0;j<=i;j++) { for (k=0;k<m;k++) { t[k] = (j==0)?0:-sum[j-1][k]; t[k] +=sum[i][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (maxsumline(t,m)>tmp) {tmp=maxsumline(t,m);} if (tmp>totalmax) { totalmax= tmp; x1=j;x2=i; } if (expand) { for (k=0;k<m;k++) { t[k]=sum[n-1][k]-sum[i][k]+sum[j][k]; } tmp = cycle?maxsumcycle(t,m):maxsumline(t,m); if (tmp>totalmax) { totalmax= tmp; x1=i;x2=j; } } } } for (k=0;k<m;k++) { tmp=0; for (i=x1;;i=(i+1)%n) { tmp+=a[i*m+k]; if (i==x2) {break;} } t[k]=tmp; } tmp = maxsumline(t,m); if (cycle) { if (maxsumcycle(t,m)>tmp) { tmp=maxsumcycle(t,m); } else { system("pause"); maxsumline(t,m); } } for (i=x1;;i=(i+1)%n) { for (j=y1;;j=(j+1)%m) { a[i*m+j]=-10001; if (j==y2) {break;} } if (i==x2) {break;} } return totalmax; }