#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
const int mxN = 2e3 + 5;
ll n,m,a[mxN][mxN],b[mxN][mxN],x,y,p[mxN][mxN];
bool vis[mxN][mxN];
bool turn(ll mid){
swap(x, y);
y = n - y + 1;
for(int j = 1; j <= m; j++){
for(int i = n; i >= 1; i--){
b[j][n - i + 1] = a[i][j];
}
}
swap(n, m);
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
a[i][j] = b[i][j];
p[i][j] = max(p[i - 1][j], p[i][j - 1]);
p[i][j] = max(p[i][j], a[i][j]);
vis[i][j] = false;
}
}
if(p[x][y] - a[x][y] > mid) return false;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(p[i][j] - a[x][y] > mid) break;
vis[i][j] = true;
}
}
ll mn = 1e17, mx = 0;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
if(vis[i][j]) continue;
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
}
return (mx - mn <= mid);
}
bool ok(ll mid){
return (turn(mid) or turn(mid) or turn(mid) or turn(mid));
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
ll mn = 1e17;
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
cin >> a[i][j];
if(mn > a[i][j]){
x = i;
y = j;
mn = a[i][j];
}
}
}
ll l = 0, r = 2e9;
while(r > l + 1){
ll mid = l + (r - l) / 2;
if(ok(mid)) r = mid;
else l = mid;
}
cout << r;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
608 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
608 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
3292 KB |
Output is correct |
16 |
Correct |
10 ms |
4300 KB |
Output is correct |
17 |
Correct |
11 ms |
4504 KB |
Output is correct |
18 |
Correct |
13 ms |
4504 KB |
Output is correct |
19 |
Correct |
16 ms |
4492 KB |
Output is correct |
20 |
Correct |
13 ms |
4240 KB |
Output is correct |
21 |
Correct |
17 ms |
4444 KB |
Output is correct |
22 |
Correct |
15 ms |
4612 KB |
Output is correct |
23 |
Correct |
16 ms |
4624 KB |
Output is correct |
24 |
Correct |
24 ms |
4536 KB |
Output is correct |
25 |
Correct |
18 ms |
4620 KB |
Output is correct |
26 |
Correct |
17 ms |
4616 KB |
Output is correct |
27 |
Correct |
17 ms |
4500 KB |
Output is correct |
28 |
Correct |
18 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
0 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
464 KB |
Output is correct |
11 |
Correct |
0 ms |
604 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
608 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
3292 KB |
Output is correct |
16 |
Correct |
10 ms |
4300 KB |
Output is correct |
17 |
Correct |
11 ms |
4504 KB |
Output is correct |
18 |
Correct |
13 ms |
4504 KB |
Output is correct |
19 |
Correct |
16 ms |
4492 KB |
Output is correct |
20 |
Correct |
13 ms |
4240 KB |
Output is correct |
21 |
Correct |
17 ms |
4444 KB |
Output is correct |
22 |
Correct |
15 ms |
4612 KB |
Output is correct |
23 |
Correct |
16 ms |
4624 KB |
Output is correct |
24 |
Correct |
24 ms |
4536 KB |
Output is correct |
25 |
Correct |
18 ms |
4620 KB |
Output is correct |
26 |
Correct |
17 ms |
4616 KB |
Output is correct |
27 |
Correct |
17 ms |
4500 KB |
Output is correct |
28 |
Correct |
18 ms |
4444 KB |
Output is correct |
29 |
Correct |
2041 ms |
120580 KB |
Output is correct |
30 |
Correct |
2020 ms |
120028 KB |
Output is correct |
31 |
Correct |
2177 ms |
121432 KB |
Output is correct |
32 |
Correct |
1975 ms |
121504 KB |
Output is correct |
33 |
Correct |
1824 ms |
117960 KB |
Output is correct |
34 |
Correct |
2189 ms |
121708 KB |
Output is correct |
35 |
Correct |
2997 ms |
137204 KB |
Output is correct |
36 |
Correct |
2545 ms |
131596 KB |
Output is correct |
37 |
Correct |
3001 ms |
137324 KB |
Output is correct |