#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
typedef long long ll;
const int NMAX = 2005;
struct p{
int x, y, d, f;
bool operator<(const p & t)const{return d > t.d;}
};
int h, w, a[NMAX][NMAX], ans, mx, mn, L[NMAX], R[NMAX], b[NMAX][NMAX];
vector<p> v;
void rotate(){
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++)
b[j][h + 1 - i] = a[i][j];
swap(w, h);
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++) a[i][j] = b[i][j];
return;
}
int solve(){
v.clear();
memset(R, 0x3f, sizeof(R));
memset(L, 0, sizeof(L));
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++) {
int d = max(mx - a[i][j], a[i][j] - mn);
int f = (d == a[i][j] - mn);
v.push_back({j, i, d, f});
}
sort(all(v));
for(auto&t : v){
int x = t.x, y = t.y, d = t.d, f = t.f;
if(f){
int r = max(x, L[y]);
for(int i = y; i <= h; i++){
if(L[i] >= r) break;
L[i] = r;
if(R[i] <= r) return d;
}
}
else{
R[y] = min(R[y], x);
if(L[y] >= R[y]) return d;
}
}
int val = a[v.back().y][v.back().x];
return min(val - mn, mx - val);
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ans = mn = 2e9;
cin >> h >> w;
for(int i = 1; i <= h; i++)
for(int j = 1; j <= w; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
for(int _ = 0; _ < 4; _++){
ans = min(ans, solve());
rotate();
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
340 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 |
2004 KB |
Output is correct |
16 |
Correct |
9 ms |
3024 KB |
Output is correct |
17 |
Correct |
18 ms |
3280 KB |
Output is correct |
18 |
Correct |
18 ms |
3248 KB |
Output is correct |
19 |
Correct |
17 ms |
3280 KB |
Output is correct |
20 |
Correct |
16 ms |
3152 KB |
Output is correct |
21 |
Correct |
18 ms |
3384 KB |
Output is correct |
22 |
Correct |
19 ms |
3408 KB |
Output is correct |
23 |
Correct |
21 ms |
3408 KB |
Output is correct |
24 |
Correct |
16 ms |
3296 KB |
Output is correct |
25 |
Correct |
18 ms |
3408 KB |
Output is correct |
26 |
Correct |
18 ms |
3408 KB |
Output is correct |
27 |
Correct |
18 ms |
3416 KB |
Output is correct |
28 |
Correct |
18 ms |
3392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
340 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 |
2004 KB |
Output is correct |
16 |
Correct |
9 ms |
3024 KB |
Output is correct |
17 |
Correct |
18 ms |
3280 KB |
Output is correct |
18 |
Correct |
18 ms |
3248 KB |
Output is correct |
19 |
Correct |
17 ms |
3280 KB |
Output is correct |
20 |
Correct |
16 ms |
3152 KB |
Output is correct |
21 |
Correct |
18 ms |
3384 KB |
Output is correct |
22 |
Correct |
19 ms |
3408 KB |
Output is correct |
23 |
Correct |
21 ms |
3408 KB |
Output is correct |
24 |
Correct |
16 ms |
3296 KB |
Output is correct |
25 |
Correct |
18 ms |
3408 KB |
Output is correct |
26 |
Correct |
18 ms |
3408 KB |
Output is correct |
27 |
Correct |
18 ms |
3416 KB |
Output is correct |
28 |
Correct |
18 ms |
3392 KB |
Output is correct |
29 |
Correct |
1705 ms |
91556 KB |
Output is correct |
30 |
Correct |
1631 ms |
90088 KB |
Output is correct |
31 |
Correct |
1741 ms |
94468 KB |
Output is correct |
32 |
Correct |
1787 ms |
94640 KB |
Output is correct |
33 |
Correct |
1636 ms |
86572 KB |
Output is correct |
34 |
Correct |
1778 ms |
94576 KB |
Output is correct |
35 |
Correct |
2173 ms |
94792 KB |
Output is correct |
36 |
Correct |
1867 ms |
86620 KB |
Output is correct |
37 |
Correct |
2116 ms |
94696 KB |
Output is correct |