#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) begin(a),end(a)
const int N = (int)2e3+2;
int n, m, a[N][N], b[N][N], c[N][N], dp[N][N];
void Rotate(){
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
c[i][j] = b[i][j];
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
b[j][i] = c[i][m-j-1];
swap(n,m);
}
bool calc(){
int bst=-1,bst2,lst,sum;
for(int i = 0; i < n; i++){
bst2=-1; lst=0; sum = 0;
for(int j = m-1; j>=0; j--){
if(b[i][j]==1){ lst=j+1; break; }
}
for(int j = 0; j < lst; j++)
dp[i][j]=0, sum+=(b[i][j]<=1);
for(int j = lst; j <= m; j++){
if(sum!=j){
for(int k = j; k <= m; k++)dp[i][k]=0;
break;
}
if(!i) dp[i][j]=(j!=m);
else if(bst!=-1) dp[i][j]=(bst<=j);
else dp[i][j]=0;
if(dp[i][j] and bst2==-1) bst2=j;
sum+=(b[i][j]<=1);
}
bst=bst2;
}
bool ok = 0;
for(int i = 1; i <= m; i++) ok|=dp[n-1][i];
return ok;
}
int mn = (int)1e9, mx = 0;
bool chk(int R){
memset(b,0,sizeof(b));
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
if(a[i][j]>mn+R) b[i][j]=2;
if(a[i][j]<mx-R){
if(b[i][j]==2) return 0;
b[i][j] = 1;
}
}
}
bool ok = 0;
for(int i = 0; i < 4; i++)
ok|=calc(), Rotate();
return ok;
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> a[i][j],mn=min(mn,a[i][j]),mx=max(mx,a[i][j]);
int l = 0, r = mx-mn;
while(l<r){
int mid = (l+r)/2;
if(chk(mid)) r=mid;
else l=mid+1;
}
cout << l << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18768 KB |
Output is correct |
2 |
Correct |
3 ms |
18768 KB |
Output is correct |
3 |
Correct |
13 ms |
18936 KB |
Output is correct |
4 |
Correct |
14 ms |
18768 KB |
Output is correct |
5 |
Correct |
13 ms |
18768 KB |
Output is correct |
6 |
Correct |
12 ms |
19096 KB |
Output is correct |
7 |
Correct |
21 ms |
18948 KB |
Output is correct |
8 |
Correct |
14 ms |
16720 KB |
Output is correct |
9 |
Correct |
27 ms |
16800 KB |
Output is correct |
10 |
Correct |
25 ms |
16208 KB |
Output is correct |
11 |
Correct |
27 ms |
16208 KB |
Output is correct |
12 |
Correct |
24 ms |
16208 KB |
Output is correct |
13 |
Correct |
26 ms |
16208 KB |
Output is correct |
14 |
Correct |
21 ms |
16208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18768 KB |
Output is correct |
2 |
Correct |
3 ms |
18768 KB |
Output is correct |
3 |
Correct |
13 ms |
18936 KB |
Output is correct |
4 |
Correct |
14 ms |
18768 KB |
Output is correct |
5 |
Correct |
13 ms |
18768 KB |
Output is correct |
6 |
Correct |
12 ms |
19096 KB |
Output is correct |
7 |
Correct |
21 ms |
18948 KB |
Output is correct |
8 |
Correct |
14 ms |
16720 KB |
Output is correct |
9 |
Correct |
27 ms |
16800 KB |
Output is correct |
10 |
Correct |
25 ms |
16208 KB |
Output is correct |
11 |
Correct |
27 ms |
16208 KB |
Output is correct |
12 |
Correct |
24 ms |
16208 KB |
Output is correct |
13 |
Correct |
26 ms |
16208 KB |
Output is correct |
14 |
Correct |
21 ms |
16208 KB |
Output is correct |
15 |
Correct |
13 ms |
17744 KB |
Output is correct |
16 |
Correct |
20 ms |
18768 KB |
Output is correct |
17 |
Correct |
36 ms |
19024 KB |
Output is correct |
18 |
Correct |
23 ms |
19024 KB |
Output is correct |
19 |
Correct |
28 ms |
19024 KB |
Output is correct |
20 |
Correct |
38 ms |
18768 KB |
Output is correct |
21 |
Correct |
40 ms |
19100 KB |
Output is correct |
22 |
Correct |
28 ms |
23632 KB |
Output is correct |
23 |
Correct |
35 ms |
25168 KB |
Output is correct |
24 |
Correct |
15 ms |
25168 KB |
Output is correct |
25 |
Correct |
29 ms |
25168 KB |
Output is correct |
26 |
Correct |
25 ms |
25200 KB |
Output is correct |
27 |
Correct |
31 ms |
25048 KB |
Output is correct |
28 |
Correct |
27 ms |
25168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
18768 KB |
Output is correct |
2 |
Correct |
3 ms |
18768 KB |
Output is correct |
3 |
Correct |
13 ms |
18936 KB |
Output is correct |
4 |
Correct |
14 ms |
18768 KB |
Output is correct |
5 |
Correct |
13 ms |
18768 KB |
Output is correct |
6 |
Correct |
12 ms |
19096 KB |
Output is correct |
7 |
Correct |
21 ms |
18948 KB |
Output is correct |
8 |
Correct |
14 ms |
16720 KB |
Output is correct |
9 |
Correct |
27 ms |
16800 KB |
Output is correct |
10 |
Correct |
25 ms |
16208 KB |
Output is correct |
11 |
Correct |
27 ms |
16208 KB |
Output is correct |
12 |
Correct |
24 ms |
16208 KB |
Output is correct |
13 |
Correct |
26 ms |
16208 KB |
Output is correct |
14 |
Correct |
21 ms |
16208 KB |
Output is correct |
15 |
Correct |
13 ms |
17744 KB |
Output is correct |
16 |
Correct |
20 ms |
18768 KB |
Output is correct |
17 |
Correct |
36 ms |
19024 KB |
Output is correct |
18 |
Correct |
23 ms |
19024 KB |
Output is correct |
19 |
Correct |
28 ms |
19024 KB |
Output is correct |
20 |
Correct |
38 ms |
18768 KB |
Output is correct |
21 |
Correct |
40 ms |
19100 KB |
Output is correct |
22 |
Correct |
28 ms |
23632 KB |
Output is correct |
23 |
Correct |
35 ms |
25168 KB |
Output is correct |
24 |
Correct |
15 ms |
25168 KB |
Output is correct |
25 |
Correct |
29 ms |
25168 KB |
Output is correct |
26 |
Correct |
25 ms |
25200 KB |
Output is correct |
27 |
Correct |
31 ms |
25048 KB |
Output is correct |
28 |
Correct |
27 ms |
25168 KB |
Output is correct |
29 |
Correct |
1010 ms |
62392 KB |
Output is correct |
30 |
Correct |
996 ms |
63176 KB |
Output is correct |
31 |
Correct |
1191 ms |
62928 KB |
Output is correct |
32 |
Correct |
1014 ms |
63184 KB |
Output is correct |
33 |
Correct |
452 ms |
62188 KB |
Output is correct |
34 |
Correct |
1071 ms |
63048 KB |
Output is correct |
35 |
Correct |
1744 ms |
63176 KB |
Output is correct |
36 |
Correct |
859 ms |
62928 KB |
Output is correct |
37 |
Correct |
1867 ms |
63180 KB |
Output is correct |