#include <bits/stdc++.h>
using namespace std;
#define FOR(i, j, k) for(int i=(j); i<=(k); i++)
#define FFOR(i, j, k) for(int i=(j); i<(k); i++)
#define DFOR(i, j, k) for(int i=(j); i>=(k); i--)
#define bug(x) cerr<<#x<<" = "<<(x)<<'\n'
#define pb push_back
#define mp make_pair
#define bit(s, i) (((s)>>(i))&1LL)
#define mask(i) ((1LL<<(i)))
#define builtin_popcount __builtin_popcountll
#define __builtin_popcount __builtin_popcountll
using ll=long long; using ld=long double;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); const ld pi=acos(0)*2;
template <typename T> inline void read(T &x){char c; bool nega=0; while((!isdigit(c=getchar()))&&(c!='-')); if(c=='-'){nega=1; c=getchar();} x=c-48; while(isdigit(c=getchar())) x=x*10+c-48; if(nega) x=-x;}
template <typename T> inline void writep(T x){if(x>9) writep(x/10); putchar(x%10+48);}
template <typename T> inline void write(T x){if(x<0){ putchar('-'); x=-x;} writep(x);}
template <typename T> inline void writeln(T x){write(x); putchar('\n');}
#define taskname "Kingdom"
int h, w;
int a[2001][2001];
int mx, mi=1e9;
int f[2001][2001];
bool check(int dist){
FOR(i, 1, h) FOR(j, 1, w) f[i][j]=0;
FOR(i, 1, h) FOR(j, 1, w){
if(mx-a[i][j]<=dist) f[i][j]^=1;
if(a[i][j]-mi<=dist) f[i][j]^=2;
if(f[i][j]==0) return 0;
}
{
bool goodlr=1;
bool goodrl=1;
int nowl=0;
int nowr=w+1;
FOR(i, 1, h){
int l=0, r=w+1;
DFOR(j, w, 1) if(f[i][j]==1){
l=j;
break;
}
FOR(j, 1, w) if(f[i][j]==2){
r=j-1;
break;
}
// cerr<<"a "<<i<<' '<<l<<' '<<r<<'\n';
nowl=max(l, nowl);
if(nowl>r){
goodlr=0;
if(goodrl==0) break;
}
nowr=min(r, nowr);
if(nowr<l){
goodrl=0;
if(goodlr==0) break;
}
}
if(goodrl||goodlr) return 1;
}
{
bool goodlr=1;
bool goodrl=1;
int nowl=0;
int nowr=w+1;
FOR(i, 1, h){
int l=0, r=w+1;
DFOR(j, w, 1) if(f[i][j]==2){
l=j;
break;
}
FOR(j, 1, w) if(f[i][j]==1){
r=j-1;
break;
}
// cerr<<"b "<<i<<' '<<l<<' '<<r<<'\n';
nowl=max(l, nowl);
if(nowl>r){
goodlr=0;
if(goodrl==0) break;
}
nowr=min(r, nowr);
if(nowr<l){
goodrl=0;
if(goodlr==0) break;
}
}
if(goodrl||goodlr) return 1;
}
return 0;
}
int main(){
#ifdef Aria
if(fopen(taskname".in", "r"))
freopen(taskname".in", "r", stdin);
#endif // Aria
read(h);
read(w);
FOR(i, 1, h) FOR(j, 1, w){
read(a[i][j]);
if(a[i][j]>mx) mx=a[i][j];
if(a[i][j]<mi) mi=a[i][j];
}
int ans=mx-mi;
int low=0, high=ans-1, mid;
// bug(check(11));
// bug(check(11));
// bug(check(11));
// bug(check(11));
// bug(check(11));
while(low<=high){
mid=(low+high)/2;
// bug(mid);
// bug(check(mid));
if(check(mid)){
ans=mid;
high=mid-1;
}
else low=mid+1;
}
writeln(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
404 KB |
Output is correct |
15 |
Correct |
10 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
2304 KB |
Output is correct |
17 |
Correct |
9 ms |
2560 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
10 ms |
2560 KB |
Output is correct |
20 |
Correct |
11 ms |
2304 KB |
Output is correct |
21 |
Correct |
11 ms |
2688 KB |
Output is correct |
22 |
Correct |
9 ms |
2688 KB |
Output is correct |
23 |
Correct |
12 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2432 KB |
Output is correct |
25 |
Correct |
15 ms |
2688 KB |
Output is correct |
26 |
Correct |
10 ms |
2688 KB |
Output is correct |
27 |
Correct |
11 ms |
2688 KB |
Output is correct |
28 |
Correct |
20 ms |
2732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
3 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
2 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
404 KB |
Output is correct |
15 |
Correct |
10 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
2304 KB |
Output is correct |
17 |
Correct |
9 ms |
2560 KB |
Output is correct |
18 |
Correct |
7 ms |
2560 KB |
Output is correct |
19 |
Correct |
10 ms |
2560 KB |
Output is correct |
20 |
Correct |
11 ms |
2304 KB |
Output is correct |
21 |
Correct |
11 ms |
2688 KB |
Output is correct |
22 |
Correct |
9 ms |
2688 KB |
Output is correct |
23 |
Correct |
12 ms |
2688 KB |
Output is correct |
24 |
Correct |
8 ms |
2432 KB |
Output is correct |
25 |
Correct |
15 ms |
2688 KB |
Output is correct |
26 |
Correct |
10 ms |
2688 KB |
Output is correct |
27 |
Correct |
11 ms |
2688 KB |
Output is correct |
28 |
Correct |
20 ms |
2732 KB |
Output is correct |
29 |
Correct |
494 ms |
52260 KB |
Output is correct |
30 |
Correct |
552 ms |
53232 KB |
Output is correct |
31 |
Correct |
669 ms |
54748 KB |
Output is correct |
32 |
Correct |
462 ms |
54784 KB |
Output is correct |
33 |
Correct |
291 ms |
48044 KB |
Output is correct |
34 |
Correct |
613 ms |
55032 KB |
Output is correct |
35 |
Correct |
753 ms |
70500 KB |
Output is correct |
36 |
Correct |
494 ms |
65344 KB |
Output is correct |
37 |
Correct |
1022 ms |
70616 KB |
Output is correct |