# Blue Bridge Cup – Max subarray

Time：2021-1-12

For more articles, please pay attention to the official account of “BLOG of the sea”.

# problem

Given a matrix A of nxm, find a nonempty submatrix in a to make the sum of elements in the submatrix maximum.
The submatrix of a refers to a block in which the row and column are continuous.
Input format
The first line of input contains two integers n and m, which respectively represent the number of rows and columns of matrix A.
Next, n rows, m integers in each row, represent matrix A.
Output format
The output line contains an integer representing the sum of elements in the largest submatrix in a.

## sample input

``````3 3
-1 -4 3
3 4 -1
-5 -2 8``````

`10`

## Example description

Take the last column, and 10.
Data scale and agreement
For 50% data, 1 < = n, m < = 50;
For 100% data, 1 < = n, m < = 500, the absolute value of each element in a is not more than 5000.

# thinking

I use dynamic programming to solve this problem, as shown in the figure below, assuming that the result of the largest submatrix is a submatrix from row r to row K and from column I to column J, as shown below (ARI represents a [R] [i], assuming that the subscript of the array starts from 1)

``````| a11 …… a1i ……a1j ……a1n |
| a21 …… a2i ……a2j ……a2n |
|  ......................|
| ...................... |
| ar1 …… ari ……arj ……arn |
|  ......................|
| ...................... |
| ak1 …… aki ……akj ……akn |
|  ......................|
| an1 …… ani ……anj ……ann |``````

Then we add up the same columns in each row from row r to row K to get a one-dimensional array as follows:
`(ar1+……+ak1, ar2+……+ak2, ……,arn+……+akn)`Then we can transform the problem of finding submatrix into the problem of finding the maximum sum of sub segments, from which we can find the solution. So the question is again, what is the largest segment sum? How to find the maximum sum of subsets?
First of all, let’s look at a question

`Given the sequence of N integers (which may be negative), a [1], a [2], a [3] , a [n], such as a [i] + a [i + 1] + +The maximum value of subsegment sum of a [J]`
For example, when (A1, A2, A3, A4, A4, A6) = (- 1,11, – 1,13, – 5, – 2), the maximum sum of sub segments is 23.

## The dynamic algorithm is used to solve the problem

**`b[j]=max{a[i]+a[j]},1<=i<=j`, and`1<=j<=n`Then the maximum sum of sub segments is Max B [J], 1 < = J < = n.
It can be seen from the definition of B [J]`When B [J-1] > 0, B [J] = B [J-1] + a [J]`Otherwise`b[j]=a[j]`。 So the dynamic programming recursive formula of B [J] is as follows:
`b[j]=max(b[j-1]+a[j],a[j])，1<=j<=n`

## Maximum sub segment sum algorithm

``````Int getmaxarray (int a [] int n) {// find the maximum sum of sub segments
int max=a[0],temp=0;
for (int i=0;i0) {
temp+=a[i];
}else {
temp=a[i];
}
max=max>temp?max:temp;
}
return  max;
}``````

# Implementation code

``````#include "stdio.h"
#include
int dp[100];
Int getmaxarray (int a [] int n) {// find the maximum sum of sub segments
int max=a[0],temp=0;
for (int i=0;i0) {
temp+=a[i];
}else {
temp=a[i];
}
max=max>temp?max:temp;
}
return  max;
}
int main(){
int n,m;
scanf("%d%d",&n,&m);
int a[n][m];
for(int i=0;i tmp ? res : tmp;
}
}
printf("%d\n", res);
}``````

For more articles, please pay attention to the official account of “BLOG of the sea”.

## Rust and python: why rust can replace Python

In this guide, we compare the rust and python programming languages. We will discuss the applicable use cases in each case, review the advantages and disadvantages of using rust and python, and explain why rust might replace python. I will introduce the following: What is rust? What is Python? When to use rust When to […]