-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmax_sum_2d.cpp
More file actions
40 lines (34 loc) · 1.31 KB
/
max_sum_2d.cpp
File metadata and controls
40 lines (34 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int A[N][N];
int main()
{
int nn, maxSubRect, subRect;
// O(n^4) DP solution, ~0.076s in UVa
scanf("%d", &nn); // the dimension of input square matrix
for(int i = 0; i < nn; i++){
for(int j = 0; j < nn; j++){
scanf("%d", &A[i][j]);
if(i > 0) A[i][j] += A[i - 1][j]; // if possible, add from top
if(j > 0) A[i][j] += A[i][j - 1]; // if possible, add from left
if(i > 0 && j > 0) A[i][j] -= A[i - 1][j - 1]; // avoid double count
} // inclusion-exclusion principle
}
maxSubRect = -127*100*100; // the lowest possible value for this problem
for(int i = 0; i < nn; i++){
for(int j = 0; j < nn; j++){ // start coordinate
for(int k = i; k < nn; k++){
for(int l = j; l < nn; l++){ // end coord
subRect = A[k][l]; // sum of all items from (0, 0) to (k, l): O(1)
if(i > 0) subRect -= A[i - 1][l]; // O(1)
if(j > 0) subRect -= A[k][j - 1]; // O(1)
if(i > 0 && j > 0) subRect += A[i - 1][j - 1]; // O(1)
maxSubRect = max(maxSubRect, subRect); // the answer is here
}
}
}
}
printf("%d\n", maxSubRect);
return 0;
}