#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
#define x first
#define y second
const int INF=1e9;
int n,m;
vector<vector<int>> v;
vector<vector<int>> premin,premax;
vector<vector<int>> sufmin,sufmax;
bool check1(int r,int l){
vector<int> a(n+1),b(n+1),a1(n+1),b1(n+1);
for(int i=1;i<=n;i++){
a[i]=upper_bound(all(premax[i]),r)-premax[i].begin()-1;
}
for(int i=1;i<=n;i++){
b[i]=lower_bound(all(sufmin[i]),l)-sufmin[i].begin();
}
a1=a;
b1=b;
for(int i=2;i<=n;i++){
a[i]=min(a[i],a[i-1]);
}
for(int i=n-1;i>0;i--){
b[i]=max(b[i],b[i+1]);
}
bool flag=1;
for(int i=1;i<=n;i++){
if(b[i]-a[i]>1){
flag=0;
}
}
if(flag==1){
return 1;
}
flag=1;
a=a1,b=b1;
for(int i=n-1;i>0;i--){
a[i]=min(a[i],a[i+1]);
}
for(int i=2;i<=n;i++){
b[i]=max(b[i],b[i-1]);
}
for(int i=1;i<=n;i++){
if(b[i]-a[i]>1){
flag=0;
}
}
return flag;
}
bool check2(int l,int r){
vector<int> a(n+1),b(n+1),a1(n+1),b1(n+1);
for(int i=1;i<=n;i++){
a[i]=upper_bound(all(premin[i]),l,greater<int>())-premin[i].begin()-1;
}
for(int i=1;i<=n;i++){
b[i]=lower_bound(all(sufmax[i]),r,greater<int>())-sufmax[i].begin();
}
a1=a,b1=b;
for(int i=2;i<=n;i++){
a[i]=min(a[i],a[i-1]);
}
for(int i=n-1;i>0;i--){
b[i]=max(b[i],b[i+1]);
}
bool flag=1;
for(int i=1;i<=n;i++){
if(b[i]-a[i]>1){
flag=0;
}
}
if(flag==1){
return 1;
}
flag=1;
a=a1,b=b1;
for(int i=n-1;i>0;i--){
a[i]=min(a[i],a[i+1]);
}
for(int i=2;i<=n;i++){
b[i]=max(b[i],b[i-1]);
}
for(int i=1;i<=n;i++){
if(b[i]-a[i]>1){
flag=0;
}
}
return flag;
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
v.resize(n+2,vector<int>(m+2));
premin.resize(n+2,vector<int>(m+2,INF));
premax.resize(n+2,vector<int>(m+2));
sufmin.resize(n+2,vector<int>(m+2,INF));
sufmax.resize(n+2,vector<int>(m+2));
int mx=-INF,mn=INF;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>v[i][j];
premax[i][j]=max(premax[i][j-1],v[i][j]);
premin[i][j]=min(premin[i][j-1],v[i][j]);
mn=min(mn,v[i][j]);
mx=max(mx,v[i][j]);
}
premin[i][m+1]=-INF;
premax[i][m+1]=INF;
}
for(int i=1;i<=n;i++){
for(int j=m;j>=1;j--){
sufmin[i][j]=min(sufmin[i][j+1],v[i][j]);
sufmax[i][j]=max(sufmax[i][j+1],v[i][j]);
}
sufmin[i][0]=-INF;
sufmax[i][0]=INF;
sufmin[i][m+1]=INF;
sufmax[i][m+1]=-INF;
}
int ans=INF;
int l=-1,r=mx-mn+1;
while(r-l>1){
int m=(l+r)>>1;
if(check1(mn+m,mx-m)){
r=m;
}
else{
l=m;
}
}
ans=min(ans,r);
l=-1,r=mx-mn+1;
while(r-l>1){
int m=(l+r)>>1;
if(check2(mx-m,mn+m)==1){
r=m;
}
else{
l=m;
}
}
ans=min(ans,r);
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1864 KB |
Output is correct |
17 |
Correct |
5 ms |
2132 KB |
Output is correct |
18 |
Correct |
5 ms |
2132 KB |
Output is correct |
19 |
Correct |
5 ms |
2132 KB |
Output is correct |
20 |
Correct |
5 ms |
2008 KB |
Output is correct |
21 |
Correct |
6 ms |
2380 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2328 KB |
Output is correct |
24 |
Correct |
6 ms |
2004 KB |
Output is correct |
25 |
Correct |
6 ms |
2256 KB |
Output is correct |
26 |
Correct |
7 ms |
2320 KB |
Output is correct |
27 |
Correct |
6 ms |
2348 KB |
Output is correct |
28 |
Correct |
6 ms |
2324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
316 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
4 ms |
1864 KB |
Output is correct |
17 |
Correct |
5 ms |
2132 KB |
Output is correct |
18 |
Correct |
5 ms |
2132 KB |
Output is correct |
19 |
Correct |
5 ms |
2132 KB |
Output is correct |
20 |
Correct |
5 ms |
2008 KB |
Output is correct |
21 |
Correct |
6 ms |
2380 KB |
Output is correct |
22 |
Correct |
6 ms |
2260 KB |
Output is correct |
23 |
Correct |
7 ms |
2328 KB |
Output is correct |
24 |
Correct |
6 ms |
2004 KB |
Output is correct |
25 |
Correct |
6 ms |
2256 KB |
Output is correct |
26 |
Correct |
7 ms |
2320 KB |
Output is correct |
27 |
Correct |
6 ms |
2348 KB |
Output is correct |
28 |
Correct |
6 ms |
2324 KB |
Output is correct |
29 |
Correct |
355 ms |
149964 KB |
Output is correct |
30 |
Correct |
342 ms |
146792 KB |
Output is correct |
31 |
Correct |
372 ms |
158028 KB |
Output is correct |
32 |
Correct |
388 ms |
158348 KB |
Output is correct |
33 |
Correct |
321 ms |
138188 KB |
Output is correct |
34 |
Correct |
368 ms |
158300 KB |
Output is correct |
35 |
Correct |
485 ms |
180992 KB |
Output is correct |
36 |
Correct |
425 ms |
171164 KB |
Output is correct |
37 |
Correct |
486 ms |
196536 KB |
Output is correct |