#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 100005;
const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int SQ = 300;
int n, m;
int mn = 1'000'000'000, mx = 0;
int a[2005][2005];
void flipx(){
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m / 2; ++j)
swap(a[i][j], a[i][m + 1 - j]);
}
void flipy(){
for(int i = 1; i <= n / 2; ++i)
for(int j = 1; j <= m; ++j)
swap(a[i][j], a[n + 1 - i][j]);
}
bool check(int x){
int lim = m;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= lim; ++j){
if(a[i][j] < mx - x){
lim = j - 1;
break;
}
}
for(int j = lim + 1; j <= m; ++j){
if(a[i][j] > mn + x)
return 0;
}
}
return 1;
}
int solve(){
int l = 0, r = 1'000'000'000;
while(l != r){
int m = (l + r) >> 1;
if(check(m))
r = m;
else
l = m + 1;
}
return l;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= m; ++j){
cin>>a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int res = 1'000'000'000;
res = min(res, solve());
flipx();
res = min(res, solve());
flipy();
res = min(res, solve());
flipx();
res = min(res, solve());
cout<<res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
1228 KB |
Output is correct |
17 |
Correct |
9 ms |
1484 KB |
Output is correct |
18 |
Correct |
9 ms |
1484 KB |
Output is correct |
19 |
Correct |
9 ms |
1484 KB |
Output is correct |
20 |
Correct |
7 ms |
1356 KB |
Output is correct |
21 |
Correct |
11 ms |
1612 KB |
Output is correct |
22 |
Correct |
13 ms |
1612 KB |
Output is correct |
23 |
Correct |
9 ms |
1668 KB |
Output is correct |
24 |
Correct |
8 ms |
1528 KB |
Output is correct |
25 |
Correct |
10 ms |
1616 KB |
Output is correct |
26 |
Correct |
9 ms |
1612 KB |
Output is correct |
27 |
Correct |
8 ms |
1628 KB |
Output is correct |
28 |
Correct |
9 ms |
1620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
324 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
328 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
2 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
324 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
1228 KB |
Output is correct |
17 |
Correct |
9 ms |
1484 KB |
Output is correct |
18 |
Correct |
9 ms |
1484 KB |
Output is correct |
19 |
Correct |
9 ms |
1484 KB |
Output is correct |
20 |
Correct |
7 ms |
1356 KB |
Output is correct |
21 |
Correct |
11 ms |
1612 KB |
Output is correct |
22 |
Correct |
13 ms |
1612 KB |
Output is correct |
23 |
Correct |
9 ms |
1668 KB |
Output is correct |
24 |
Correct |
8 ms |
1528 KB |
Output is correct |
25 |
Correct |
10 ms |
1616 KB |
Output is correct |
26 |
Correct |
9 ms |
1612 KB |
Output is correct |
27 |
Correct |
8 ms |
1628 KB |
Output is correct |
28 |
Correct |
9 ms |
1620 KB |
Output is correct |
29 |
Correct |
611 ms |
37192 KB |
Output is correct |
30 |
Correct |
638 ms |
37528 KB |
Output is correct |
31 |
Correct |
663 ms |
39136 KB |
Output is correct |
32 |
Correct |
657 ms |
39132 KB |
Output is correct |
33 |
Correct |
551 ms |
34056 KB |
Output is correct |
34 |
Correct |
665 ms |
39216 KB |
Output is correct |
35 |
Correct |
757 ms |
54768 KB |
Output is correct |
36 |
Correct |
643 ms |
49580 KB |
Output is correct |
37 |
Correct |
683 ms |
54920 KB |
Output is correct |