#include <bits/stdc++.h>
using namespace std;
const int nax = 2001;
const int max_a = 1000000000;
int mat[nax][nax];
bool curr[nax][nax];
int mn = max_a, mx = 0;
int n, m;
void print() {
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cout << mat[i][j] << " ";
}
cout << "\n";
}
cout << "\n";
}
void mini(int& a, int b) {
a = min(a, b);
}
bool gali(int k) {
int idx = 0;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
// max priklauso [mx-k, mx]
if (mx - k > mat[i][j]) {
// (i, j) butinai priklauso TIK minimumams
idx = max(idx, j+1);
}
}
// visi tarp tures buti minimumai [mn, mn+k]
for(int j = 0; j < idx; j++) {
if (mat[i][j] > mn + k) {
return false;
}
}
}
return true;
}
void flip1() {
int aux[m][n];
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
aux[i][j] = mat[j][m-i-1];
//cout << i << " " << j << ": " << j << " " << n-i-1 << " " << aux[i][j] << "\n";
}
}
swap(n, m);
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++){
mat[i][j] = aux[i][j];
}
}
}
int go() {
int l = 0, r = max_a, ans = max_a;
while(l <= r) {
int mid = (l + r) / 2;
if (gali(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
return ans;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
cin >> mat[i][j];
}
}
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
mn = min(mn, mat[i][j]);
mx = max(mx, mat[i][j]);
}
}
//cout << mn << " " << mx << "\n";
int ans = go();
flip1();
//print();
mini(ans, go());
flip1();
//print();
mini(ans, go());
flip1();
//print();
mini(ans, go());
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
5 ms |
1364 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
19 |
Correct |
6 ms |
1364 KB |
Output is correct |
20 |
Correct |
6 ms |
1364 KB |
Output is correct |
21 |
Correct |
6 ms |
1364 KB |
Output is correct |
22 |
Correct |
6 ms |
1364 KB |
Output is correct |
23 |
Correct |
9 ms |
1364 KB |
Output is correct |
24 |
Correct |
5 ms |
1488 KB |
Output is correct |
25 |
Correct |
9 ms |
1364 KB |
Output is correct |
26 |
Correct |
6 ms |
1364 KB |
Output is correct |
27 |
Correct |
6 ms |
1364 KB |
Output is correct |
28 |
Correct |
6 ms |
1364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1108 KB |
Output is correct |
16 |
Correct |
5 ms |
1364 KB |
Output is correct |
17 |
Correct |
6 ms |
1364 KB |
Output is correct |
18 |
Correct |
6 ms |
1364 KB |
Output is correct |
19 |
Correct |
6 ms |
1364 KB |
Output is correct |
20 |
Correct |
6 ms |
1364 KB |
Output is correct |
21 |
Correct |
6 ms |
1364 KB |
Output is correct |
22 |
Correct |
6 ms |
1364 KB |
Output is correct |
23 |
Correct |
9 ms |
1364 KB |
Output is correct |
24 |
Correct |
5 ms |
1488 KB |
Output is correct |
25 |
Correct |
9 ms |
1364 KB |
Output is correct |
26 |
Correct |
6 ms |
1364 KB |
Output is correct |
27 |
Correct |
6 ms |
1364 KB |
Output is correct |
28 |
Correct |
6 ms |
1364 KB |
Output is correct |
29 |
Correct |
521 ms |
30932 KB |
Output is correct |
30 |
Correct |
525 ms |
30572 KB |
Output is correct |
31 |
Correct |
605 ms |
31660 KB |
Output is correct |
32 |
Correct |
560 ms |
31592 KB |
Output is correct |
33 |
Correct |
476 ms |
29508 KB |
Output is correct |
34 |
Correct |
618 ms |
31692 KB |
Output is correct |
35 |
Correct |
537 ms |
31544 KB |
Output is correct |
36 |
Correct |
485 ms |
29528 KB |
Output is correct |
37 |
Correct |
614 ms |
31636 KB |
Output is correct |