forked from mpfeifer1/Kattis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpirate.cpp
104 lines (85 loc) · 2.29 KB
/
pirate.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>
using namespace std;
typedef long long ll;
ll m, n;
ll a, b;
vector<vector<ll>> v;
vector<pair<ll,vector<ll>>> getArrays() {
// Res contains pairs of {rows, array of mins}
vector<pair<ll,vector<ll>>> res;
// For each top row
for(ll i = 0; i < n; i++) {
vector<ll> currmin = v[i];
// For each bottom row
for(ll j = i; j < n; j++) {
// Keep track of our running min
for(ll k = 0; k < m; k++) {
currmin[k] = min(currmin[k],v[j][k]);
}
// Add this array
res.push_back({j-i+1,currmin});
}
}
// Return all the arrays
return res;
}
ll calc(ll r, ll c, ll depth) {
ll top = depth * (n*m);
ll bot = (n*m) - (r*c);
assert(bot > 0);
if(top % bot == 0) {
return (top / bot - 1) * r * c;
}
return (top / bot) * r * c;
}
ll getval(ll rows, vector<ll>& arr) {
vector<ll> lefts(m), rights(m);
stack<ll> positions;
for(ll i = 0; i < m; ++i) {
while(!positions.empty() && arr[positions.top()] >= arr[i]) positions.pop();
if(positions.empty()) {
lefts[i] = -1;
} else {
lefts[i] = positions.top();
}
positions.push(i);
}
while(!positions.empty()) positions.pop();
for(ll i = m-1; i >= 0; --i) {
while(!positions.empty() && arr[positions.top()] >= arr[i]) positions.pop();
if(positions.empty()) {
rights[i] = m;
} else {
rights[i] = positions.top();
}
positions.push(i);
}
ll best = 0;
for(ll i = 0; i < m; i++) {
ll cols = rights[i] - lefts[i] - 1;
best = max(best, calc(min(a,rows),min(b,cols),arr[i]));
best = max(best, calc(min(b,rows),min(a,cols),arr[i]));
}
return best;
}
void solve() {
ll best = 0;
vector<pair<ll,vector<ll>>> arrs = getArrays();
int t = 0;
for(auto& i : arrs) {
best = max(best,getval(i.first,i.second));
}
cout << best << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> a >> b >> n >> m;
v.resize(n,vector<ll>(m));
for(ll i = 0; i < n; i++) {
for(ll j = 0; j < m; j++) {
cin >> v[i][j];
}
}
solve();
}