#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;
}
// bug(mx);
// bug(mi);
// FOR(i, 1, h) FOR(j, 1, w) cerr<<a[i][j]<<" \n"[j==w];
// FOR(i, 1, h) FOR(j, 1, w) cerr<<f[i][j]<<" \n"[j==w];
{
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;
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;
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 |
2 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 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 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 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 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 |
Incorrect |
2 ms |
384 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |