#include <bits/stdc++.h>
#define maxn (int)(1e5+51)
#define all(x) x.begin(), x.end()
#define sz(x) (int) x.size()
#define endl '\n'
#define ll long long
#define pb push_back
#define ull unsigned long long
#define ii pair<int,int>
#define iii tuple<int,int,int>
#define inf 2000000001
#define mod 1000000007 //998244353
#define _ ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
using namespace std;
template<typename X, typename Y> bool ckmin(X& x, const Y& y) { return (y < x) ? (x=y,1):0; }
template<typename X, typename Y> bool ckmax(X& x, const Y& y) { return (x < y) ? (x=y,1):0; }
int grid[2001][2001],n,m,okA[2001][2001],okB[2001][2001],mxA[2001],mxB[2001],mnA[2001],mnB[2001];
int mx = 0,mn = inf;
bool possible(int x){
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
okA[i][j] = grid[i][j] - mn <= x, okB[i][j] = mx - grid[i][j] <= x;
for(int i=1;i<=n;++i)mxA[i] = mxB[i] = 0, mnA[i] = mnB[i] = inf;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
if(!okA[i][j]&&!okB[i][j])return false;
if(!okA[i][j]&&okB[i][j])ckmax(mxB[i],j), ckmin(mnB[i],j);
if(okA[i][j]&&!okB[i][j])ckmax(mxA[i],j), ckmin(mnA[i],j);
}
}
bool ok1 = 1, ok2 = 1;//ok1: A cresce, ok2: B cresce
int mxxA=0, mxxB=0;
for(int i=1;i<=n;++i){
if(mnB[i]<mxA[i])ok1 = 0;
if(mnA[i]<mxB[i])ok2 = 0;
if(mnB[i]<=mxxA)ok1 = 0;
if(mnA[i]<=mxxB)ok2 = 0;
ckmax(mxxA,mxA[i]);
ckmax(mxxB,mxB[i]);
}
if(ok1+ok2)return true;
ok1 = ok2 = 1, mxxA = mxxB = m+1;
for(int i=1;i<=n;++i){
if(mnB[i]<mxA[i])ok1 = 0;
if(mnA[i]<mxB[i])ok2 = 0;
if(mxA[i]>mxxA)ok1 = 0;
if(mxB[i]>mxxB)ok2 = 0;
ckmin(mxxA,mnB[i]-1);
ckmin(mxxB,mnA[i]-1);
}
return ok1+ok2;
}
int main(){_
cin>>n>>m;
for(int i=1;i<=n;++i){
for(int j=1;j<=m;++j){
cin>>grid[i][j];
ckmax(mx,grid[i][j]);
ckmin(mn,grid[i][j]);
}
}
int l = 0, r = mx - mn, ans = r;
while(l<=r){
int md = l + (r-l+1)/2;
if(possible(md))ans = md, r = md - 1;
else l = md + 1;
}
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
3148 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3148 KB |
Output is correct |
19 |
Correct |
8 ms |
3104 KB |
Output is correct |
20 |
Correct |
10 ms |
3020 KB |
Output is correct |
21 |
Correct |
8 ms |
3472 KB |
Output is correct |
22 |
Correct |
8 ms |
3516 KB |
Output is correct |
23 |
Correct |
11 ms |
3532 KB |
Output is correct |
24 |
Correct |
7 ms |
3148 KB |
Output is correct |
25 |
Correct |
17 ms |
3592 KB |
Output is correct |
26 |
Correct |
9 ms |
3524 KB |
Output is correct |
27 |
Correct |
8 ms |
3496 KB |
Output is correct |
28 |
Correct |
9 ms |
3524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
460 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
460 KB |
Output is correct |
9 |
Correct |
1 ms |
460 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
460 KB |
Output is correct |
12 |
Correct |
1 ms |
460 KB |
Output is correct |
13 |
Correct |
0 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
3 ms |
3148 KB |
Output is correct |
17 |
Correct |
7 ms |
3164 KB |
Output is correct |
18 |
Correct |
7 ms |
3148 KB |
Output is correct |
19 |
Correct |
8 ms |
3104 KB |
Output is correct |
20 |
Correct |
10 ms |
3020 KB |
Output is correct |
21 |
Correct |
8 ms |
3472 KB |
Output is correct |
22 |
Correct |
8 ms |
3516 KB |
Output is correct |
23 |
Correct |
11 ms |
3532 KB |
Output is correct |
24 |
Correct |
7 ms |
3148 KB |
Output is correct |
25 |
Correct |
17 ms |
3592 KB |
Output is correct |
26 |
Correct |
9 ms |
3524 KB |
Output is correct |
27 |
Correct |
8 ms |
3496 KB |
Output is correct |
28 |
Correct |
9 ms |
3524 KB |
Output is correct |
29 |
Correct |
507 ms |
67088 KB |
Output is correct |
30 |
Correct |
559 ms |
68852 KB |
Output is correct |
31 |
Correct |
692 ms |
70444 KB |
Output is correct |
32 |
Correct |
554 ms |
70436 KB |
Output is correct |
33 |
Correct |
354 ms |
61636 KB |
Output is correct |
34 |
Correct |
621 ms |
70580 KB |
Output is correct |
35 |
Correct |
708 ms |
86076 KB |
Output is correct |
36 |
Correct |
584 ms |
80768 KB |
Output is correct |
37 |
Correct |
956 ms |
86204 KB |
Output is correct |