//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 2010;
int n,m;
int grid[maxn][maxn];
int mn=mod,mx;
int pos[maxn];
int ans = 0;
bool check(int dif) {
//mn on the top
int lst = n-1;
rep(i,0,m) {
int cur = -1;
while (cur+1<=lst and grid[cur+1][i]-mn<=dif) cur++;
pos[i] = cur;
lst = cur;
}
bool ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (mx-grid[i][j]>dif) ok=true;
}
if (!ok) return true;
lst = n-1;
for (int i=m-1;i>=0;i--) {
int cur = -1;
while (cur+1<=lst and grid[cur+1][i]-mn<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (mx-grid[i][j]>dif) ok=true;
}
if (!ok) return true;
//mx on the top
lst = n-1;
rep(i,0,m) {
int cur = -1;
while (cur+1<=lst and mx-grid[cur+1][i]<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (grid[i][j]-mn>dif) ok=true;
}
if (!ok) return true;
lst = n-1;
for (int i=m-1;i>=0;i--) {
int cur = -1;
while (cur+1<=lst and mx-grid[cur+1][i]<=dif) cur++;
pos[i] = cur;
lst = cur;
}
ok = false;
rep(j,0,m) {
rep(i,pos[j]+1,n) if (grid[i][j]-mn>dif) ok=true;
}
return !ok;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
rep(i,0,n)
rep(j,0,m) cin>>grid[i][j],mn = min(mn,grid[i][j]),mx = max(mx,grid[i][j]);
int l = 0,r=1e9;
while (l<r) {
int mid=l+r>>1;
if (check(mid)) r=mid;
else l=mid+1;
}
cout<<l;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:97:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
97 | int mid=l+r>>1;
| ~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1296 KB |
Output is correct |
17 |
Correct |
8 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
1500 KB |
Output is correct |
19 |
Correct |
8 ms |
1576 KB |
Output is correct |
20 |
Correct |
9 ms |
1440 KB |
Output is correct |
21 |
Correct |
10 ms |
1620 KB |
Output is correct |
22 |
Correct |
10 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1604 KB |
Output is correct |
24 |
Correct |
8 ms |
1492 KB |
Output is correct |
25 |
Correct |
9 ms |
1624 KB |
Output is correct |
26 |
Correct |
11 ms |
1620 KB |
Output is correct |
27 |
Correct |
9 ms |
1692 KB |
Output is correct |
28 |
Correct |
10 ms |
1688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1296 KB |
Output is correct |
17 |
Correct |
8 ms |
1492 KB |
Output is correct |
18 |
Correct |
7 ms |
1500 KB |
Output is correct |
19 |
Correct |
8 ms |
1576 KB |
Output is correct |
20 |
Correct |
9 ms |
1440 KB |
Output is correct |
21 |
Correct |
10 ms |
1620 KB |
Output is correct |
22 |
Correct |
10 ms |
1620 KB |
Output is correct |
23 |
Correct |
9 ms |
1604 KB |
Output is correct |
24 |
Correct |
8 ms |
1492 KB |
Output is correct |
25 |
Correct |
9 ms |
1624 KB |
Output is correct |
26 |
Correct |
11 ms |
1620 KB |
Output is correct |
27 |
Correct |
9 ms |
1692 KB |
Output is correct |
28 |
Correct |
10 ms |
1688 KB |
Output is correct |
29 |
Correct |
1641 ms |
37356 KB |
Output is correct |
30 |
Correct |
2075 ms |
37576 KB |
Output is correct |
31 |
Correct |
2532 ms |
39200 KB |
Output is correct |
32 |
Correct |
2282 ms |
39156 KB |
Output is correct |
33 |
Correct |
1368 ms |
34184 KB |
Output is correct |
34 |
Correct |
2197 ms |
39272 KB |
Output is correct |
35 |
Correct |
3403 ms |
54728 KB |
Output is correct |
36 |
Correct |
2314 ms |
49532 KB |
Output is correct |
37 |
Correct |
3016 ms |
54936 KB |
Output is correct |