#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) {
memset(cnt, 0, sizeof(cnt));
int prev = INF;
owo(i, 0, h) {
int curr = -1;
owo(j, 0, w) {
//cout<<i<<" "<<j<<" "<<comp[board[i][j]]<<"\n";
if(mx-comp[board[i][j]]<=val&&j<=prev) {
cnt[board[i][j]]++;
curr = j;
continue;
}
break;
}
prev = curr;
}
uwu(i, idx, 0) {
if(cnt[i]==save[i]) {
if(i==0) return true;
continue;
}
if(comp[i]-mn<=val)return true;
break;
}
memset(cnt, 0, sizeof(cnt));
prev = -1;
owo(i, 0, w) {
int curr = INF;
uwu(j, h, 0) {
if(mx-comp[board[j][i]]<=val&&j>=prev) {
cnt[board[j][i]]++;
curr = j;
continue;
}
break;
}
prev = curr;
}
uwu(i, idx, 0) {
if(cnt[i]==save[i]) {
if(i==0) return true;
continue;
}
if(comp[i]-mn<=val)return true;
break;
}
memset(cnt, 0, sizeof(cnt));
prev = -1;
uwu(i, h, 0) {
int curr = INF;
uwu(j, w, 0) {
if(mx-comp[board[i][j]]<=val&&j>=prev) {
cnt[board[i][j]]++;
curr = j;
continue;
}
break;
}
prev = curr;
}
uwu(i, idx, 0) {
if(cnt[i]==save[i]) {
if(i==0) return true;
continue;
}
if(comp[i]-mn<=val)return true;
break;
}
memset(cnt, 0, sizeof(cnt));
prev = INF;
uwu(i, w, 0) {
int curr = -1;
owo(j, 0, h) {
if(mx-comp[board[j][i]]<=val&&j<=prev) {
cnt[board[j][i]]++;
curr = j;
continue;
}
break;
}
prev = curr;
}
uwu(i, idx, 0) {
if(cnt[i]==save[i]) {
if(i==0) return true;
continue;
}
if(comp[i]-mn<=val)return true;
break;
}
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 |
33 ms |
16000 KB |
Output is correct |
2 |
Correct |
23 ms |
16000 KB |
Output is correct |
3 |
Correct |
200 ms |
16120 KB |
Output is correct |
4 |
Correct |
216 ms |
16120 KB |
Output is correct |
5 |
Correct |
164 ms |
16128 KB |
Output is correct |
6 |
Correct |
266 ms |
16120 KB |
Output is correct |
7 |
Correct |
180 ms |
16120 KB |
Output is correct |
8 |
Correct |
248 ms |
16008 KB |
Output is correct |
9 |
Correct |
257 ms |
16124 KB |
Output is correct |
10 |
Correct |
263 ms |
16120 KB |
Output is correct |
11 |
Correct |
187 ms |
16120 KB |
Output is correct |
12 |
Correct |
185 ms |
16120 KB |
Output is correct |
13 |
Correct |
190 ms |
16248 KB |
Output is correct |
14 |
Correct |
146 ms |
16120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
16000 KB |
Output is correct |
2 |
Correct |
23 ms |
16000 KB |
Output is correct |
3 |
Correct |
200 ms |
16120 KB |
Output is correct |
4 |
Correct |
216 ms |
16120 KB |
Output is correct |
5 |
Correct |
164 ms |
16128 KB |
Output is correct |
6 |
Correct |
266 ms |
16120 KB |
Output is correct |
7 |
Correct |
180 ms |
16120 KB |
Output is correct |
8 |
Correct |
248 ms |
16008 KB |
Output is correct |
9 |
Correct |
257 ms |
16124 KB |
Output is correct |
10 |
Correct |
263 ms |
16120 KB |
Output is correct |
11 |
Correct |
187 ms |
16120 KB |
Output is correct |
12 |
Correct |
185 ms |
16120 KB |
Output is correct |
13 |
Correct |
190 ms |
16248 KB |
Output is correct |
14 |
Correct |
146 ms |
16120 KB |
Output is correct |
15 |
Correct |
32 ms |
16128 KB |
Output is correct |
16 |
Correct |
31 ms |
17168 KB |
Output is correct |
17 |
Correct |
196 ms |
21368 KB |
Output is correct |
18 |
Correct |
206 ms |
21240 KB |
Output is correct |
19 |
Correct |
158 ms |
21368 KB |
Output is correct |
20 |
Correct |
158 ms |
20728 KB |
Output is correct |
21 |
Correct |
242 ms |
21496 KB |
Output is correct |
22 |
Correct |
192 ms |
21368 KB |
Output is correct |
23 |
Correct |
193 ms |
21496 KB |
Output is correct |
24 |
Correct |
229 ms |
20984 KB |
Output is correct |
25 |
Correct |
194 ms |
21504 KB |
Output is correct |
26 |
Correct |
258 ms |
21624 KB |
Output is correct |
27 |
Correct |
294 ms |
21496 KB |
Output is correct |
28 |
Correct |
226 ms |
21500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
16000 KB |
Output is correct |
2 |
Correct |
23 ms |
16000 KB |
Output is correct |
3 |
Correct |
200 ms |
16120 KB |
Output is correct |
4 |
Correct |
216 ms |
16120 KB |
Output is correct |
5 |
Correct |
164 ms |
16128 KB |
Output is correct |
6 |
Correct |
266 ms |
16120 KB |
Output is correct |
7 |
Correct |
180 ms |
16120 KB |
Output is correct |
8 |
Correct |
248 ms |
16008 KB |
Output is correct |
9 |
Correct |
257 ms |
16124 KB |
Output is correct |
10 |
Correct |
263 ms |
16120 KB |
Output is correct |
11 |
Correct |
187 ms |
16120 KB |
Output is correct |
12 |
Correct |
185 ms |
16120 KB |
Output is correct |
13 |
Correct |
190 ms |
16248 KB |
Output is correct |
14 |
Correct |
146 ms |
16120 KB |
Output is correct |
15 |
Correct |
32 ms |
16128 KB |
Output is correct |
16 |
Correct |
31 ms |
17168 KB |
Output is correct |
17 |
Correct |
196 ms |
21368 KB |
Output is correct |
18 |
Correct |
206 ms |
21240 KB |
Output is correct |
19 |
Correct |
158 ms |
21368 KB |
Output is correct |
20 |
Correct |
158 ms |
20728 KB |
Output is correct |
21 |
Correct |
242 ms |
21496 KB |
Output is correct |
22 |
Correct |
192 ms |
21368 KB |
Output is correct |
23 |
Correct |
193 ms |
21496 KB |
Output is correct |
24 |
Correct |
229 ms |
20984 KB |
Output is correct |
25 |
Correct |
194 ms |
21504 KB |
Output is correct |
26 |
Correct |
258 ms |
21624 KB |
Output is correct |
27 |
Correct |
294 ms |
21496 KB |
Output is correct |
28 |
Correct |
226 ms |
21500 KB |
Output is correct |
29 |
Correct |
2659 ms |
63428 KB |
Output is correct |
30 |
Correct |
2566 ms |
63768 KB |
Output is correct |
31 |
Correct |
3266 ms |
65212 KB |
Output is correct |
32 |
Correct |
2811 ms |
65400 KB |
Output is correct |
33 |
Correct |
2211 ms |
58756 KB |
Output is correct |
34 |
Correct |
2915 ms |
65184 KB |
Output is correct |
35 |
Execution timed out |
4014 ms |
169776 KB |
Time limit exceeded |
36 |
Halted |
0 ms |
0 KB |
- |