#include <bits/stdc++.h>
#define pb push_back
#define s second
#define f first
#define pb push_back
#define pii pair <int,int>
#define ll long long
#pragma GCC optimize("Ofast")
using namespace std;
const int N = 2e3 + 3;
int a[N][N],n,m,mn,mx,cnt[3][N];
bool vis[N][N],cur[3][N][N];
void go(int x,int y,int d){
if (x < 1 || y < 1 || x > n || y > m || vis[x][y]) return;
if (a[x][y] - mn > d) return;
vis[x][y] = 1;
go(x+1,y,d);
go(x-1,y,d);
go(x,y+1,d);
go(x,y-1,d);
}
bool get(bool k,int d){
int mn1=-1,mx1=-1,mn2=-1,mx2=-1;
for (int i= 1; i <= n; i++){
for (int j= 1;j<=m;j++){
if(cur[k][i][j]){
if (mn1 == -1) {
mn1 = mx1 = a[i][j];
}else{
mn1 = min(mn1,a[i][j]);
mx1 = max(mx1,a[i][j]);
}
}else{
if (mn2 == -1) {
mn2 = mx2 = a[i][j];
}else{
mn2 = min(mn2,a[i][j]);
mx2 = max(mx2,a[i][j]);
}
}
if (mx2-mn2 > d) return 0;
if (mx1-mn1 > d) return 0;
}
}
return 1;
}
bool go1(int d){
int mn0 = 2e9,mn1 = 2e9,l0,r0,l1,r1;
for (int i = 1; i <= n; i++){
mn0 = min(mn0,cnt[0][i]);
mn1 = min(mn1,cnt[1][i]);
l0 = m - mn0 + 1; r0 = m;
l1 = 1; r1 = mn1;
for (int j = 1; j <= m; j++){
cur[1][i][j] = (j >= l1 && j <= r1);
cur[0][i][j] = (j >= l0 && j <= r0);
}
}
return (get(0,d)) || (get(1,d));
}
bool go2(int d){
int mn0 = 2e9,mn1 = 2e9,l0,r0,l1,r1;
for (int i = n; i >= 1; i--){
mn0 = min(mn0,cnt[0][i]);
mn1 = min(mn1,cnt[1][i]);
l0 = m - mn0 + 1; r0 = m;
l1 = 1; r1 = mn1;
for (int j = 1; j <= m; j++){
cur[1][i][j] = (j >= l1 && j <= r1);
cur[0][i][j] = (j >= l0 && j <= r0);
}
}
return (get(0,d)) || (get(1,d));
}
signed main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
cin>>n>>m;
mn = 2e9;
int X = 0,Y=0;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= m; j++){
cin >> a[i][j];
if (a[i][j] < mn){
X=i,Y=j;
mn=a[i][j];
}
mx=max(mx,a[i][j]);
}
}
int l = 0,r = mx - mn,ans=mx-mn;
while (l <= r){
int mid=(l+r)/2;
go(X,Y,mid);
for (int i = 1; i <= n; i++){
cnt[0][i] = m;
for (int j = m; j >= 1; j--){
if (!vis[i][j]){
cnt[0][i] = m - j;
break;
}
}
cnt[1][i] = m;
for (int j = 1; j <= m; j++){
if (!vis[i][j]){
cnt[1][i] = j - 1;
break;
}
}
for (int j = 1; j <= m; j++)
vis[i][j] = 0;
}
if (go1(mid) || go2(mid)){
ans = mid;
r = mid-1;
}else{
l = mid + 1;
}
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
6 ms |
8536 KB |
Output is correct |
17 |
Correct |
19 ms |
6096 KB |
Output is correct |
18 |
Correct |
12 ms |
7256 KB |
Output is correct |
19 |
Correct |
12 ms |
5464 KB |
Output is correct |
20 |
Correct |
17 ms |
7004 KB |
Output is correct |
21 |
Correct |
29 ms |
6748 KB |
Output is correct |
22 |
Correct |
17 ms |
4956 KB |
Output is correct |
23 |
Correct |
16 ms |
5464 KB |
Output is correct |
24 |
Correct |
16 ms |
5212 KB |
Output is correct |
25 |
Correct |
17 ms |
4884 KB |
Output is correct |
26 |
Correct |
27 ms |
7508 KB |
Output is correct |
27 |
Correct |
30 ms |
6492 KB |
Output is correct |
28 |
Correct |
31 ms |
7096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
4492 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
0 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
2648 KB |
Output is correct |
16 |
Correct |
6 ms |
8536 KB |
Output is correct |
17 |
Correct |
19 ms |
6096 KB |
Output is correct |
18 |
Correct |
12 ms |
7256 KB |
Output is correct |
19 |
Correct |
12 ms |
5464 KB |
Output is correct |
20 |
Correct |
17 ms |
7004 KB |
Output is correct |
21 |
Correct |
29 ms |
6748 KB |
Output is correct |
22 |
Correct |
17 ms |
4956 KB |
Output is correct |
23 |
Correct |
16 ms |
5464 KB |
Output is correct |
24 |
Correct |
16 ms |
5212 KB |
Output is correct |
25 |
Correct |
17 ms |
4884 KB |
Output is correct |
26 |
Correct |
27 ms |
7508 KB |
Output is correct |
27 |
Correct |
30 ms |
6492 KB |
Output is correct |
28 |
Correct |
31 ms |
7096 KB |
Output is correct |
29 |
Correct |
1887 ms |
184780 KB |
Output is correct |
30 |
Correct |
1758 ms |
186200 KB |
Output is correct |
31 |
Correct |
1356 ms |
144464 KB |
Output is correct |
32 |
Correct |
2044 ms |
229728 KB |
Output is correct |
33 |
Correct |
1051 ms |
108120 KB |
Output is correct |
34 |
Correct |
1956 ms |
206488 KB |
Output is correct |
35 |
Correct |
3785 ms |
225440 KB |
Output is correct |
36 |
Correct |
2303 ms |
173636 KB |
Output is correct |
37 |
Correct |
3412 ms |
234064 KB |
Output is correct |