-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies.cpp
104 lines (96 loc) · 2.61 KB
/
movies.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <bits/stdc++.h>
long long areaof(int x1, int y1, int x2, int y2, const std::vector<std::vector<long long>>& dp)
{
// Make sure x1,y1 is the bottom-right corner
if (x1 < x2)
std::swap(x1, x2);
if (y1 < y2)
std::swap(y1, y2);
// Handle the case where x2 or y2 is zero properly to avoid out of bounds access
long long total = dp[x1][y1];
long long left = (x2 > 0) ? dp[x2-1][y1] : 0;
long long top = (y2 > 0) ? dp[x1][y2-1] : 0;
long long corner = (x2 > 0 && y2 > 0) ? dp[x2-1][y2-1] : 0;
return total - left - top + corner;
}
int main()
{
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
int n, m;
std::cin >> n >> m;
std::vector<std::vector<char>> a(n, std::vector<char>(m, 0));
std::vector<std::vector<long long>> dp(n+1, std::vector<long long>(m+1, 0));
std::vector<std::pair<int,int>> i_ler;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
char c;
std::cin >> c;
a[i][j] = c;
if (c == 'i')
i_ler.push_back(std::make_pair(i+1, j+1));
}
}
int max_x = 0;
int max_y = 0;
int max_dp = INT_MIN;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
dp[i][j] = dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];
if (a[i-1][j-1] == 'a')
{
dp[i][j]++;
}
else if (a[i-1][j-1] == 'b')
{
dp[i][j]--;
}
if (dp[i][j] > max_dp)
{
max_dp = dp[i][j];
max_x = i;
max_y = j;
}
}
}
/*
// print dp
for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
{
std::cout << dp[i][j] << " ";
}
std::cout << std::endl;
}
*/
int selected_x = 0;
int selected_y = 0;
// std::cout << areaof(3, 3, 2, 2, dp);
long long max = 0;
for (auto [ix, iy] : i_ler)
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if (i == ix && j == iy) continue;
long long area = areaof(i, j, ix, iy, dp);
if (area > max)
{
max = area;
selected_x = i;
selected_y = j;
}
// max = std::max(max, areaof(i, j, max_x, max_y, dp));
}
}
}
std::cout << max << std::endl;
// std::cout << selected_x << " " << selected_y << std::endl;
return 0;
}