#include <bits/stdc++.h>
#define owo(i,a, b) for(int i=(a); i<(b); ++i)
#define uwu(i,a, b) for(int i=(a)-1; i>=(b); --i)
#define senpai push_back
#define ttgl pair<int, int>
#define ayaya cout<<"ayaya~"<<endl
using namespace std;
using ll = long long;
using ld = long double;
const ll MOD = 998244353;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll binpow(ll a,ll b){ll res=1;while(b){if(b&1)res=(res*a)%MOD;a=(a*a)%MOD;b>>=1;}return res;}
ll modInv(ll a){return binpow(a, MOD-2);}
const double PI = acos(-1);
const int INF = 0x3f3f3f3f;
const int NINF = 0xc0c0c0c0;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const ll NINFLL = 0xc0c0c0c0c0c0c0c0;
const int mxN = 2001;
int comp[mxN*mxN];
int board[mxN][mxN];
int cnt[mxN*mxN];
int save[mxN*mxN];
map<int, int>mp;
set<int> aaa;
int w, h;
int mx = 0;
int mn = INF;
int idx = 0;
bool check(int val) {
int prev = INF;
bool ok = true;
owo(i, 0, h) {
ok = true;
int curr = -1;
owo(j, 0, w) {
//cout<<i<<" "<<j<<" "<<comp[board[i][j]]<<"\n";
if(mx-comp[board[i][j]]<=val&&j<=prev) {
curr = j;
continue;
}
owo(k, j, w) {
if(comp[board[i][k]]-mn>val) {
ok = false;
break;
}
}
break;
}
if(!ok)break;
prev = curr;
if(i==h-1)return true;
}
prev = -1;
owo(i, 0, w) {
ok = true;
int curr = INF;
uwu(j, h, 0) {
if(mx-comp[board[j][i]]<=val&&j>=prev) {
curr = j;
continue;
}
uwu(k, j, 0) {
if(comp[board[k][i]]-mn>val) {
ok = false;
break;
}
}
break;
}
if(!ok)break;
prev = curr;
if(i==w-1)return true;
}
prev = -1;
uwu(i, h, 0) {
ok = true;
int curr = INF;
uwu(j, w, 0) {
if(mx-comp[board[i][j]]<=val&&j>=prev) {
curr = j;
continue;
}
uwu(k, j, 0) {
if(comp[board[i][k]]-mn>val) {
ok = false;
break;
}
}
break;
}
if(!ok)break;
prev = curr;
if(i==0)return true;
}
prev = INF;
uwu(i, w, 0) {
ok = true;
int curr = -1;
owo(j, 0, h) {
if(mx-comp[board[j][i]]<=val&&j<=prev) {
curr = j;
continue;
}
owo(k, j, h) {
if(comp[board[k][i]]>val) {
ok = false;
break;
}
}
break;
}
if(!ok)break;
prev = curr;
if(i==0)return true;
}
return false;
}
int main()
{
//freopen("exercise.in", "r", stdin);
//freopen("exercise.out", "w", stdout);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
cin.tie(0)->sync_with_stdio(0);
cin>>h>>w;
owo(i, 0, h) {
owo(j, 0, w) {
cin>>board[i][j];
aaa.insert(board[i][j]);
mx = max(mx, board[i][j]);
mn = min(mn, board[i][j]);
}
}
auto it = aaa.begin();
while(it!=aaa.end()) {
mp[*it] = idx;
comp[idx] = *it;
idx++;
it++;
}
owo(i, 0, h) {
owo(j, 0, w) {
board[i][j] = mp[board[i][j]];
save[board[i][j]]++;
}
}
int l = 0;
int r = mx-mn;
while(l<r) {
int mid = (l+r)/2;
if(check(mid)) {
r = mid;
}else{
l = mid+1;
}
}
cout<<l<<"\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Incorrect |
0 ms |
384 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |