#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define F first
#define S second
#define FOR(i,a,b) for (auto i = (a); i <= (b); ++i)
#define NFOR(i,a,b) for(auto i = (a); i >= (b); --i)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
#define mp(i,a) make_pair(i,a)
#define pb(a) push_back(a)
#define bit(x,b) (x&(1LL<<b))
typedef int lli;
typedef pair <lli,lli> ii;
typedef pair <lli,ii> iii;
typedef vector <lli> vi;
typedef tree<pair<lli,int>,null_type,less<pair<lli,int>>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int n,m,ma=0,mi=1000000000;
int arr[2001][2001];
ii l1[2001][2001],s1[2001][2001];
vector<pair<int,pair<ii,ii>>> v1[2001];
int check(int v,int t,int v3)
{
int ma1=mi,i,mi1=ma;
if(t<2)
i=0;
else
i=n+1;
for(int j=1;j<=m;++j)
{
auto it=lower_bound(all(v1[j]),mp(v,mp(mp(-1,-1),mp(-1,-1))))-v1[j].begin();
it--;
auto it1=lower_bound(all(v1[j]),mp(v+1,mp(mp(-1,-1),mp(-1,-1))))-v1[j].begin();
int l;
if(t==0)
{
if(it==-1)
l=0;
else
l=v1[j][it].S.F.F;
i=max(l,i);
if(i!=0&&i!=n+1)
ma1=max(ma1,l1[j][i].F);
}
else if(t==3)
{
if(it==-1)
l=n+1;
else
l=v1[j][it].S.F.S;
i=min(l,i);
if(i!=0&&i!=n+1)
ma1=max(ma1,l1[j][i].S);
}
else if(t==1)
{
if(it1==n)
l=0;
else
l=v1[j][it1].S.S.F;
i=max(l,i);
if(i!=0&&i!=n+1)
mi1=min(mi1,s1[j][i].F);
}
else
{
if(it1==n)
l=n+1;
else
l=v1[j][it1].S.S.S;
i=min(i,l);
if(i!=0&&i!=n+1)
mi1=min(mi1,s1[j][i].S);
}
}
if(t==0||t==3)
{
if(ma1<=v3)
return(1);
else
return(0);
}
else
{
if(mi1>=v3)
return(1);
else
return(0);
}
}
int solve(int v)
{
int c=check(ma-v,3,mi+v);
c=max(c,check(mi+v,2,ma-v));
c=max(c,check(ma-v,0,mi+v));
c=max(c,check(mi+v,1,ma-v));
return(c);
}
int main()
{
cin>>n>>m;
for(int i=n;i>=1;--i)
for(int j=1;j<=m;++j)
cin>>arr[i][j];
for(int j=1;j<=m;++j)
{
l1[j][0].F=0;
s1[j][0].F=1000000200;
for(int i=1;i<=n;++i)
{
ma=max(ma,arr[i][j]);
mi=min(mi,arr[i][j]);
v1[j].pb(mp(arr[i][j],mp(mp(i,i),mp(i,i))));
l1[j][i].F=max(l1[j][i-1].F,arr[i][j]);
s1[j][i].F=min(s1[j][i-1].F,arr[i][j]);
}
sort(all(v1[j]));
for(int i=1;i<n;++i)
{
v1[j][i].S.F.F=max(v1[j][i-1].S.F.F,v1[j][i].S.F.F);
v1[j][i].S.F.S=min(v1[j][i-1].S.F.S,v1[j][i].S.F.S);
}
l1[j][n+1].S=0;
s1[j][n+1].S=1000000200;
for(int i=n;i>=1;--i)
{
if(i!=n)
{
v1[j][i-1].S.S.F=max(v1[j][i].S.S.F,v1[j][i-1].S.S.F);
v1[j][i-1].S.S.S=min(v1[j][i].S.S.S,v1[j][i-1].S.S.S);
}
l1[j][i].S=max(l1[j][i+1].S,arr[i][j]);
s1[j][i].S=min(s1[j][i+1].S,arr[i][j]);
}
}
int l=0,r=ma-mi,ans;
ans=ma-mi;
while(l<=r)
{
int mid=(l+r)/2;
if(solve(mid))
{
r=mid-1;
ans=min(ans,mid);
}
else
{
l=mid+1;
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
16 |
Correct |
13 ms |
4352 KB |
Output is correct |
17 |
Correct |
26 ms |
4864 KB |
Output is correct |
18 |
Correct |
24 ms |
4856 KB |
Output is correct |
19 |
Correct |
26 ms |
4856 KB |
Output is correct |
20 |
Correct |
22 ms |
4600 KB |
Output is correct |
21 |
Correct |
32 ms |
4984 KB |
Output is correct |
22 |
Correct |
32 ms |
5004 KB |
Output is correct |
23 |
Correct |
31 ms |
4984 KB |
Output is correct |
24 |
Correct |
28 ms |
4728 KB |
Output is correct |
25 |
Correct |
32 ms |
4992 KB |
Output is correct |
26 |
Correct |
31 ms |
4984 KB |
Output is correct |
27 |
Correct |
34 ms |
4984 KB |
Output is correct |
28 |
Correct |
31 ms |
4984 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
512 KB |
Output is correct |
3 |
Correct |
1 ms |
512 KB |
Output is correct |
4 |
Correct |
1 ms |
512 KB |
Output is correct |
5 |
Correct |
1 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
512 KB |
Output is correct |
7 |
Correct |
1 ms |
512 KB |
Output is correct |
8 |
Correct |
1 ms |
512 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
1 ms |
512 KB |
Output is correct |
11 |
Correct |
1 ms |
512 KB |
Output is correct |
12 |
Correct |
1 ms |
512 KB |
Output is correct |
13 |
Correct |
1 ms |
512 KB |
Output is correct |
14 |
Correct |
1 ms |
512 KB |
Output is correct |
15 |
Correct |
2 ms |
2176 KB |
Output is correct |
16 |
Correct |
13 ms |
4352 KB |
Output is correct |
17 |
Correct |
26 ms |
4864 KB |
Output is correct |
18 |
Correct |
24 ms |
4856 KB |
Output is correct |
19 |
Correct |
26 ms |
4856 KB |
Output is correct |
20 |
Correct |
22 ms |
4600 KB |
Output is correct |
21 |
Correct |
32 ms |
4984 KB |
Output is correct |
22 |
Correct |
32 ms |
5004 KB |
Output is correct |
23 |
Correct |
31 ms |
4984 KB |
Output is correct |
24 |
Correct |
28 ms |
4728 KB |
Output is correct |
25 |
Correct |
32 ms |
4992 KB |
Output is correct |
26 |
Correct |
31 ms |
4984 KB |
Output is correct |
27 |
Correct |
34 ms |
4984 KB |
Output is correct |
28 |
Correct |
31 ms |
4984 KB |
Output is correct |
29 |
Correct |
2001 ms |
180276 KB |
Output is correct |
30 |
Correct |
1939 ms |
170460 KB |
Output is correct |
31 |
Correct |
2122 ms |
182256 KB |
Output is correct |
32 |
Correct |
2058 ms |
182292 KB |
Output is correct |
33 |
Correct |
1802 ms |
172964 KB |
Output is correct |
34 |
Correct |
2108 ms |
182516 KB |
Output is correct |
35 |
Correct |
2892 ms |
197856 KB |
Output is correct |
36 |
Correct |
2496 ms |
175472 KB |
Output is correct |
37 |
Correct |
2968 ms |
197956 KB |
Output is correct |