#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define nl << '\n'
#define sp << ' ' <<
int h, w;
int g[2000][2000], a[2000], b[2000], maxElement, minElement;
bool possible(int diff){
int res0 = 0, res1 = 0;
for(int i=0; i<h; ++i){
a[i] = 0;
for(int j=0; j<w; ++j){
if(g[i][j]-minElement<=diff) ++a[i];
else break;
}
b[i] = a[i];
a[i] = min(a[i], a[i-1]);
}
for(int i=h-2; i>=0; --i) b[i] = min(b[i], b[i+1]);
for(int i=0; i<h; ++i){
for(int j=a[i]; j<w; ++j)
res0 = max(res0, maxElement - g[i][j]);
for(int j=b[i]; j<w; ++j)
res1 = max(res1, maxElement - g[i][j]);
}
return (res0<=diff or res1<=diff);
}
int search(){
int res = maxElement - minElement;
int low = 0, high = res;
while(low<high){
int mid = (low+high)/2;
if(possible(mid)) high = mid;
else low = mid+1;
}
if(!possible(low)) return res;
return low;
}
void rotate(){
int g0[h][w];
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
g0[i][j] = g[i][j];
swap(h, w);
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
g[i][j] = g0[j][h-1-i];
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> h >> w;
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
cin >> g[i][j];
int ans = 2e9;
maxElement = minElement = g[0][0];
for(int i=0; i<h; ++i)
for(int j=0; j<w; ++j)
maxElement = max(maxElement, g[i][j]), minElement = min(minElement, g[i][j]);
// for(int __=0; __<2; ++__){
for(int _=0; _<4; ++_){
if(_) rotate();
ans = min(ans, search());
}
// for(int i=0; i<h; ++i)
// for(int j=0; j<w; ++j)
// g[i][j] = -g[i][j];
// swap(maxElement, minElement);
// maxElement = -maxElement;
// minElement = -minElement;
// }
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
1132 KB |
Output is correct |
16 |
Correct |
5 ms |
1388 KB |
Output is correct |
17 |
Correct |
9 ms |
1388 KB |
Output is correct |
18 |
Correct |
9 ms |
1388 KB |
Output is correct |
19 |
Correct |
10 ms |
1388 KB |
Output is correct |
20 |
Correct |
8 ms |
1388 KB |
Output is correct |
21 |
Correct |
11 ms |
1388 KB |
Output is correct |
22 |
Correct |
12 ms |
1388 KB |
Output is correct |
23 |
Correct |
11 ms |
1388 KB |
Output is correct |
24 |
Correct |
10 ms |
1388 KB |
Output is correct |
25 |
Correct |
11 ms |
1388 KB |
Output is correct |
26 |
Correct |
12 ms |
1388 KB |
Output is correct |
27 |
Correct |
11 ms |
1388 KB |
Output is correct |
28 |
Correct |
11 ms |
1388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
1132 KB |
Output is correct |
16 |
Correct |
5 ms |
1388 KB |
Output is correct |
17 |
Correct |
9 ms |
1388 KB |
Output is correct |
18 |
Correct |
9 ms |
1388 KB |
Output is correct |
19 |
Correct |
10 ms |
1388 KB |
Output is correct |
20 |
Correct |
8 ms |
1388 KB |
Output is correct |
21 |
Correct |
11 ms |
1388 KB |
Output is correct |
22 |
Correct |
12 ms |
1388 KB |
Output is correct |
23 |
Correct |
11 ms |
1388 KB |
Output is correct |
24 |
Correct |
10 ms |
1388 KB |
Output is correct |
25 |
Correct |
11 ms |
1388 KB |
Output is correct |
26 |
Correct |
12 ms |
1388 KB |
Output is correct |
27 |
Correct |
11 ms |
1388 KB |
Output is correct |
28 |
Correct |
11 ms |
1388 KB |
Output is correct |
29 |
Correct |
718 ms |
30828 KB |
Output is correct |
30 |
Correct |
717 ms |
30700 KB |
Output is correct |
31 |
Correct |
767 ms |
31636 KB |
Output is correct |
32 |
Correct |
759 ms |
31724 KB |
Output is correct |
33 |
Correct |
682 ms |
29736 KB |
Output is correct |
34 |
Correct |
777 ms |
31852 KB |
Output is correct |
35 |
Correct |
1116 ms |
31724 KB |
Output is correct |
36 |
Correct |
995 ms |
29548 KB |
Output is correct |
37 |
Correct |
1115 ms |
31768 KB |
Output is correct |