#include<bits/stdc++.h>
#define st first
#define nd second
using namespace std;
using ll = int;
const ll INF = 1e9+1;
int value(pair<int, int> x) {
return max(x.st, x.nd);
}
ll solve(vector<vector<ll>>& grid) {
int h = grid.size(), w = grid[0].size();
ll L = INF, R = 0;
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
L = min(L, grid[i][j]);
R = max(R, grid[i][j]);
}
}
vector<vector<pair<int, int>>> dp(h+1, vector<pair<int, int>>(w+1));
for(int i=1; i<=h; ++i) {
int mx = L;
dp[i][0] = dp[i-1][0];
for(int j=1; j<=w; ++j) {
mx = max(mx, grid[i-1][j-1]);
dp[i][j] = dp[i-1][j];
if(value(dp[i][j-1]) < value(dp[i][j])) {
dp[i][j] = dp[i][j-1];
}
dp[i][j].st = max(dp[i][j].st, mx-L);
}
int mini = R;
for(int j=w; j>=0; --j) {
dp[i][j].nd = max(dp[i][j].nd, R-mini);
if(j-1>=0) {
mini = min(mini, grid[i-1][j-1]);
}
}
}
int res = INF;
for(int i=1; i<=w; ++i) {
res = min(res, value(dp[h][i]));
}
return res;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int h, w; cin>>h>>w;
vector<vector<ll>> grid(h, vector<ll>(w));
for(int i=0; i<h; ++i) {
for(int j=0; j<w; ++j) {
cin>>grid[i][j];
}
}
ll ans = INF;
auto G = grid;
ans = min(ans, solve(G));
reverse(G.begin(), G.end());
ans = min(ans, solve(G));
for(int i=0; i<h; ++i) {
reverse(G[i].begin(), G[i].end());
}
ans = min(ans, solve(G));
reverse(G.begin(), G.end());
ans = min(ans, solve(G));
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
6 ms |
1024 KB |
Output is correct |
18 |
Correct |
6 ms |
1024 KB |
Output is correct |
19 |
Correct |
6 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
968 KB |
Output is correct |
21 |
Correct |
7 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1024 KB |
Output is correct |
23 |
Correct |
7 ms |
1024 KB |
Output is correct |
24 |
Correct |
6 ms |
940 KB |
Output is correct |
25 |
Correct |
8 ms |
1024 KB |
Output is correct |
26 |
Correct |
7 ms |
1024 KB |
Output is correct |
27 |
Correct |
7 ms |
1024 KB |
Output is correct |
28 |
Correct |
7 ms |
1024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
404 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
980 KB |
Output is correct |
17 |
Correct |
6 ms |
1024 KB |
Output is correct |
18 |
Correct |
6 ms |
1024 KB |
Output is correct |
19 |
Correct |
6 ms |
1024 KB |
Output is correct |
20 |
Correct |
5 ms |
968 KB |
Output is correct |
21 |
Correct |
7 ms |
1024 KB |
Output is correct |
22 |
Correct |
7 ms |
1024 KB |
Output is correct |
23 |
Correct |
7 ms |
1024 KB |
Output is correct |
24 |
Correct |
6 ms |
940 KB |
Output is correct |
25 |
Correct |
8 ms |
1024 KB |
Output is correct |
26 |
Correct |
7 ms |
1024 KB |
Output is correct |
27 |
Correct |
7 ms |
1024 KB |
Output is correct |
28 |
Correct |
7 ms |
1024 KB |
Output is correct |
29 |
Correct |
526 ms |
60124 KB |
Output is correct |
30 |
Correct |
509 ms |
58904 KB |
Output is correct |
31 |
Correct |
552 ms |
63224 KB |
Output is correct |
32 |
Correct |
554 ms |
63368 KB |
Output is correct |
33 |
Correct |
481 ms |
55324 KB |
Output is correct |
34 |
Correct |
550 ms |
63496 KB |
Output is correct |
35 |
Correct |
648 ms |
63356 KB |
Output is correct |
36 |
Correct |
575 ms |
89196 KB |
Output is correct |
37 |
Correct |
658 ms |
102396 KB |
Output is correct |