-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmangoTrees.cpp
More file actions
72 lines (62 loc) · 2.15 KB
/
mangoTrees.cpp
File metadata and controls
72 lines (62 loc) · 2.15 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <limits>
#include <vectors>
using namespace std;
int main ()
{
int n;
cin >> n;
int matrix[n][n];
int tot = 0;
int ySum[n];
int xSum[n];
for (int i = 0; i < n; ++i) {
currSum = 0;
for (int j = 0; j < n; ++j) {
cin >> matrix[i][j];
tot = tot + matrix[i][j];
currSum = currSum + matrix[i][j];
ySum[j] = ySum[j-1] + matrix[i][j];
};
if (i == 0) {
xSum[i] = currSum;
} else {
xSum[i] = xSum[i-1] + currSum;
};
};
int topLeft[n][n];
int topRight[n][n];
int bottomLeft[n][n];
int bottomRight[n][n];
int minima = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
topLeft[i][j] = matrix[i][j] + topLeft[i-1][j] + topLeft[i][j-1] - topLeft [i-1][j-1];
topRight[i][j] = xSum[i] - topLeft[i][j];
bottomLeft[i][j] = ySum[j] - topLeft[i][j];
bottomRight[i][j] = tot - (topRight[i][j] + bottomLeft[i][j] + topLeft[i][j]);
topLeftArea = (i+1) * (j+1);
topRightArea = (n-j) * (i+1);
bottomLeftArea = (n-i) * (j+1);
bottomRightArea = (n-j) * (n-i);
if (topLeftArea < topRightArea && topLeftArea < bottomLeftArea && topLeftArea < bottomRightArea) {
if (topLeft > minima) {
minima = topLeft;
};
} else if (topRightArea < topLeftArea && topRightArea < bottomLeftArea && topRightArea < bottomRightArea) {
if (topRight > minima) {
minima = topRight;
};
} else if (bottomLeftArea < topLeftArea && bottomLeftArea < topRightArea && bottomLeftArea < bottomRightArea) {
if (bottomLeft > minima) {
minima = bottomLeft;
};
} else if (bottomRightArea < topLeftArea && bottomRightArea < topRightArea && bottomRightArea < bottomLeftArea) {
if (bottomRight > minima) {
minima = bottomRight;
};
}
}
}
cout << minima;
}