#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> II;
const int N = 2e3 + 5, logN = 20;
const int MOD = 1e9 + 7;
const ll INF = 1e9;
int h, w, a[N][N], tmp[N][N], Min = INF, Max = 0;
void turn90(void){
for(int i = 1; i <= h; i ++)
for(int j = 1; j <= w; j ++) tmp[j][h - i + 1] = a[i][j];
swap(h, w);
for(int i = 1; i <= h; i ++) for(int j = 1; j <= w; j ++) a[i][j] = tmp[i][j];
}
bool check(int mid){
int pref = INF;
for(int i = 1; i <= h; i ++){
int len = 0;
while(len + 1 <= w && a[i][len + 1] - Min <= mid) len ++;
pref = min(pref, len);
for(int j = pref + 1; j <= w; j ++) if(Max - a[i][j] > mid) return false;
}
return true;
}
bool ok(int mid){
for(int i = 0; i < 4; i ++){
if(check(mid)) return true;
turn90();
}
return false;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> h >> w;
for(int i = 1; i <= h; i ++) for(int j = 1; j <= w; j ++) cin >> a[i][j], Min = min(Min, a[i][j]), Max = max(Max, a[i][j]);
int ans = Max - Min, l = 0, r = ans - 1;
while(l <= r){
int mid = l + r >> 1;
if(ok(mid)) ans = mid, r = mid - 1;
else l = mid + 1;
}
cout << ans << "\n";
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:44:20: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int mid = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
4 ms |
2260 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
7 ms |
2524 KB |
Output is correct |
19 |
Correct |
8 ms |
2516 KB |
Output is correct |
20 |
Correct |
6 ms |
2388 KB |
Output is correct |
21 |
Correct |
11 ms |
2628 KB |
Output is correct |
22 |
Correct |
8 ms |
2652 KB |
Output is correct |
23 |
Correct |
8 ms |
2636 KB |
Output is correct |
24 |
Correct |
7 ms |
2516 KB |
Output is correct |
25 |
Correct |
8 ms |
2600 KB |
Output is correct |
26 |
Correct |
9 ms |
2644 KB |
Output is correct |
27 |
Correct |
11 ms |
2644 KB |
Output is correct |
28 |
Correct |
16 ms |
2648 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
2 ms |
1876 KB |
Output is correct |
16 |
Correct |
4 ms |
2260 KB |
Output is correct |
17 |
Correct |
12 ms |
2516 KB |
Output is correct |
18 |
Correct |
7 ms |
2524 KB |
Output is correct |
19 |
Correct |
8 ms |
2516 KB |
Output is correct |
20 |
Correct |
6 ms |
2388 KB |
Output is correct |
21 |
Correct |
11 ms |
2628 KB |
Output is correct |
22 |
Correct |
8 ms |
2652 KB |
Output is correct |
23 |
Correct |
8 ms |
2636 KB |
Output is correct |
24 |
Correct |
7 ms |
2516 KB |
Output is correct |
25 |
Correct |
8 ms |
2600 KB |
Output is correct |
26 |
Correct |
9 ms |
2644 KB |
Output is correct |
27 |
Correct |
11 ms |
2644 KB |
Output is correct |
28 |
Correct |
16 ms |
2648 KB |
Output is correct |
29 |
Correct |
1610 ms |
31872 KB |
Output is correct |
30 |
Correct |
1407 ms |
31828 KB |
Output is correct |
31 |
Correct |
1476 ms |
31732 KB |
Output is correct |
32 |
Correct |
1671 ms |
31800 KB |
Output is correct |
33 |
Correct |
1688 ms |
31608 KB |
Output is correct |
34 |
Correct |
1782 ms |
31800 KB |
Output is correct |
35 |
Correct |
2650 ms |
31884 KB |
Output is correct |
36 |
Correct |
2024 ms |
31720 KB |
Output is correct |
37 |
Correct |
1965 ms |
31916 KB |
Output is correct |