#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]]-mn>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 |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
1280 KB |
Output is correct |
17 |
Correct |
36 ms |
5248 KB |
Output is correct |
18 |
Correct |
34 ms |
5248 KB |
Output is correct |
19 |
Correct |
34 ms |
5240 KB |
Output is correct |
20 |
Correct |
29 ms |
4728 KB |
Output is correct |
21 |
Correct |
40 ms |
5376 KB |
Output is correct |
22 |
Correct |
35 ms |
5376 KB |
Output is correct |
23 |
Correct |
43 ms |
5496 KB |
Output is correct |
24 |
Correct |
32 ms |
4864 KB |
Output is correct |
25 |
Correct |
37 ms |
5376 KB |
Output is correct |
26 |
Correct |
42 ms |
5368 KB |
Output is correct |
27 |
Correct |
40 ms |
5368 KB |
Output is correct |
28 |
Correct |
40 ms |
5320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
0 ms |
384 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
0 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
512 KB |
Output is correct |
16 |
Correct |
5 ms |
1280 KB |
Output is correct |
17 |
Correct |
36 ms |
5248 KB |
Output is correct |
18 |
Correct |
34 ms |
5248 KB |
Output is correct |
19 |
Correct |
34 ms |
5240 KB |
Output is correct |
20 |
Correct |
29 ms |
4728 KB |
Output is correct |
21 |
Correct |
40 ms |
5376 KB |
Output is correct |
22 |
Correct |
35 ms |
5376 KB |
Output is correct |
23 |
Correct |
43 ms |
5496 KB |
Output is correct |
24 |
Correct |
32 ms |
4864 KB |
Output is correct |
25 |
Correct |
37 ms |
5376 KB |
Output is correct |
26 |
Correct |
42 ms |
5368 KB |
Output is correct |
27 |
Correct |
40 ms |
5368 KB |
Output is correct |
28 |
Correct |
40 ms |
5320 KB |
Output is correct |
29 |
Correct |
2745 ms |
25604 KB |
Output is correct |
30 |
Correct |
2526 ms |
26300 KB |
Output is correct |
31 |
Correct |
2928 ms |
26236 KB |
Output is correct |
32 |
Correct |
2853 ms |
26360 KB |
Output is correct |
33 |
Correct |
2021 ms |
22904 KB |
Output is correct |
34 |
Correct |
2552 ms |
26232 KB |
Output is correct |
35 |
Execution timed out |
4059 ms |
158948 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |