topic
Thinking
If we start thinking from positive,
DP is to start thinking like a chicken, like the author of this konjac
You will find that the value of each DP needs to have four auxiliary array to transfer
But are also affect the current value of the subsequent DP DP value
But think for a train of thought,
It is easy to find the answer is monotonic
That dichotomy
The final shape of a ladder must be
Then you find the minimum value in the trapezoid, then half of the answer to find the maximum
Time is on the complexity verifier \ (O (n * m) \)
Plus binary complexity \ (O (log_ {maxa} ) \)
If 1S, there are still regular card
But the topic of human goodness opened a 4S
Code
#include<iostream>
#include<climits>
#include<cstring>
using namespace std;
int n,m;
int a[2005][2005];
int b[2005][2005];
int c[2005][2005];
int d[2005][2005];
int h[2005];
int minn=INT_MAX;
int maxx=INT_MIN;
int l=INT_MAX;
int r=INT_MIN;
int mid;
bool dfs(int x,int t[2005][2005])
{
memset(h,0,sizeof(h));
h[n+1]=m;
for(int i=n;i>=1;i--)
{
while(h[i]<h[i+1]&&t[i][h[i]+1]-minn<=x)
h[i]++;
}
int minnh=INT_MAX;
for(int i=1;i<=n;i++)
for(int j=h[i]+1;j<=m;j++)
minnh=min(minnh,t[i][j]);
if(maxx-minnh<=x)
return 1;
return 0;
}
bool pd(int cnt)
{
if(dfs(cnt,a)||dfs(cnt,b)||dfs(cnt,c)||dfs(cnt,d))
return 1;
return 0;
}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>a[i][j];
d[i][j]=c[i][j]=b[i][j]=a[i][j];
minn=min(minn,a[i][j]);
maxx=max(maxx,a[i][j]);
}
}
for(int i=1;i<=n/2;i++)
swap(b[i],b[n-i+1]);
for(int i=1;i<=n/2;i++)
swap(c[i],c[n-i+1]);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m/2;j++)
{
swap(c[i][j],c[i][m-j+1]);
swap(d[i][j],d[i][m-j+1]);
}
}
l=0;
r=maxx-minn;
while(l<r)
{
mid=(1ll*l+r)>>1;
if(pd(mid))
r=mid;
else
l=mid+1;
}
cout<<l;
return 0;
}