# 【循环数组的最大字串和】Maximal-sum Subsequence

## PROBLEM

### 题目描述

​ M2,1,M2,2,M2,3,M2,4,M2,5,M2,6,M2,7,M2,8.
​ M2,2,M2,3,M2,4.
​ M2,6,M2,7,M2,8,M2,1,M2,2.
​ M4,3,M5,3,M6,3,M7,3.
​ M1,2,M2,3,M3,4,M4,5.
​ M2,4,M3,3,M4,2,M5,1.
​ M3,3,M4,2,M5,1,M1,5. （按样例理解是M8,6 ,emmmmm）
​ M5,6.

### 样例输入

``````1
4
8 6 6 1
-3 4 0 5
4 2 1 9
1 -9 9 -2``````

### 样例输出

``24``

## CODE

``````#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int MAXN = 2005;

int martix[MAXN][MAXN];

int dp[MAXN];

int py(int x,int n){
while (x<0)x+=n;
while (x>=n)x-=n;
return x;
}

int cal(int arr[],int n) {
int sum = 0;
memset(dp, 0, sizeof(dp));
sum = dp[0] = arr[0];
for (int i = 1; i < n; i++) {
dp[i] = max(arr[i], dp[i-1]+arr[i]);
sum += arr[i];
}
int ans = 0;
for (int i = 1; i < n; i++) {
if (dp[i] > dp[ans]) {
ans = i;
}
}
int ans1 =  dp[ans];
for (int i = 0; i < n; i++) {
arr[i] = -arr[i];
}
memset(dp, 0, sizeof(dp));
dp[0] = arr[0];
for (int i = 1; i < n; i++) {
dp[i] = max(arr[i], dp[i-1]+arr[i]);
}
ans = 0;
for (int i = 1; i < n; i++) {
if (dp[i] > dp[ans]) {
ans = i;
}
}
int ans2 = dp[ans];
return max(ans1,ans2+sum);
}

int main(){
int T;
for(scanf("%d",&T);T;T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
scanf("%d",&martix[i][j]);
}
}
int ans = 0;
for(int i=0;i<n;i++){
int carry[MAXN];
for(int j=0;j<n;j++){
carry[j] = martix[i][j];
}
ans = max(ans,cal(carry,n));
for(int j=0;j<n;j++){
carry[j] = martix[j][i];
}
ans = max(ans,cal(carry,n));
for(int j=0;j<n;j++){
carry[j] = martix[py(i-j,n)][py(j,n)];
}
ans = max(ans,cal(carry,n));
for(int j=0;j<n;j++){
carry[j] = martix[py(i-j,n)][py(n-j-1,n)];
}
ans = max(ans,cal(carry,n));
}
cout<<ans<<endl;
}
}``````

0条评论