#include <bits/stdc++.h>
using namespace std;
#warning change mxn
const int mxn = 2005;
const int inf = 2e9;
int h, w;
int mxa = 0;
int a[mxn][mxn];
bool b[mxn][mxn];
int check() {
int mnj = w;
int mn = inf/2, mx = 0;
for(int i = 0; i < h; i++) {
for(int j = 0; j < mnj; j++) {
if(b[i][j]) mnj = j;
}
for(int j = mnj; j < w; j++) {
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
return mx-mn;
}
bool solve(int k) {
for(int i = 0; i < h; i++)
for(int j = 0; j < w; j++)
b[i][j] = mxa-a[i][j]>k;
bool ok = false;
// check
ok = ok || (check() <= k);
// reverse rows
for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(a[i][j], a[i][w-j-1]);
for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(b[i][j], b[i][w-j-1]);
// check
ok = ok || (check() <= k);
// reverse cols
for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(a[i][j], a[h-i-1][j]);
for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(b[i][j], b[h-i-1][j]);
// check
ok = ok || (check() <= k);
// reverse rows
for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(a[i][j], a[i][w-j-1]);
for(int i = 0; i < h; i++) for(int j = 0; j < w/2; j++) swap(b[i][j], b[i][w-j-1]);
// check
ok = ok || (check() <= k);
// reverse cols
for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(a[i][j], a[h-i-1][j]);
for(int i = 0; i < h/2; i++) for(int j = 0; j < w; j++) swap(b[i][j], b[h-i-1][j]);
return ok;
}
int main(int argc, char const *argv[]) {
cin >> h >> w;
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
cin >> a[i][j]; mxa = max(mxa, a[i][j]);
}
}
int lo = 0, hi = inf/2;
while(lo < hi) {
int m = (lo+hi)/2;
if(solve(m)) {
hi = m;
}
else {
lo = m+1;
}
}
cout << lo << "\n";
return 0;
}
Compilation message
joioi.cpp:5:2: warning: #warning change mxn [-Wcpp]
5 | #warning change mxn
| ^~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
11 ms |
1656 KB |
Output is correct |
17 |
Correct |
22 ms |
1924 KB |
Output is correct |
18 |
Correct |
18 ms |
1832 KB |
Output is correct |
19 |
Correct |
17 ms |
1928 KB |
Output is correct |
20 |
Correct |
16 ms |
1636 KB |
Output is correct |
21 |
Correct |
22 ms |
2036 KB |
Output is correct |
22 |
Correct |
20 ms |
2004 KB |
Output is correct |
23 |
Correct |
22 ms |
2004 KB |
Output is correct |
24 |
Correct |
19 ms |
1748 KB |
Output is correct |
25 |
Correct |
22 ms |
2020 KB |
Output is correct |
26 |
Correct |
21 ms |
2044 KB |
Output is correct |
27 |
Correct |
21 ms |
1972 KB |
Output is correct |
28 |
Correct |
21 ms |
1984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
380 KB |
Output is correct |
16 |
Correct |
11 ms |
1656 KB |
Output is correct |
17 |
Correct |
22 ms |
1924 KB |
Output is correct |
18 |
Correct |
18 ms |
1832 KB |
Output is correct |
19 |
Correct |
17 ms |
1928 KB |
Output is correct |
20 |
Correct |
16 ms |
1636 KB |
Output is correct |
21 |
Correct |
22 ms |
2036 KB |
Output is correct |
22 |
Correct |
20 ms |
2004 KB |
Output is correct |
23 |
Correct |
22 ms |
2004 KB |
Output is correct |
24 |
Correct |
19 ms |
1748 KB |
Output is correct |
25 |
Correct |
22 ms |
2020 KB |
Output is correct |
26 |
Correct |
21 ms |
2044 KB |
Output is correct |
27 |
Correct |
21 ms |
1972 KB |
Output is correct |
28 |
Correct |
21 ms |
1984 KB |
Output is correct |
29 |
Correct |
1693 ms |
41068 KB |
Output is correct |
30 |
Correct |
1541 ms |
41436 KB |
Output is correct |
31 |
Correct |
1728 ms |
42984 KB |
Output is correct |
32 |
Correct |
1583 ms |
42948 KB |
Output is correct |
33 |
Correct |
1425 ms |
37544 KB |
Output is correct |
34 |
Correct |
1609 ms |
43224 KB |
Output is correct |
35 |
Correct |
2161 ms |
58692 KB |
Output is correct |
36 |
Correct |
2399 ms |
53556 KB |
Output is correct |
37 |
Correct |
2066 ms |
58772 KB |
Output is correct |