//記得跳題
#include<iostream>
#include<array>
#include<vector>
#include<string>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<math.h>
#include<map>
#include<unordered_map>
#include<cstring>
#include<iomanip>
#include<bitset>
#include<tuple>
#define ll long long
#define int ll
#define LL __int128_t
#define DB double
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define mp make_pair
#define INF (ll)(2e9)
#define MOD (ll)(1e9+7)
#define F first
#define S second
#define endl "\n"
#define AC ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
struct func{
bool operator()(const int &x, const int &y) const{
return x<y;
}
};
template<class T> using PQ=priority_queue<T,vector<T>,func>;
void file(){
freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
freopen("/Users/liaoyunyang/Desktop/meta_out.txt","w",stdout);
}
int a[2][2005][2005],init[2005][2005],ih,iw,pre[2][2005][2005],suf[2][2005][2005];
int h,w,ans=INF;
void dfs(int i,int j,int max1,int min1,int max2,int min2){
if(i==h+1){
if(j==0) return;
//cout<<max1<<" "<<min1<<" "<<max2<<" "<<min2<<endl;
ans=min(ans,max(max1-min1,max2-min2)); return;
}
FOR(x,j,w+1){
dfs(i+1,x,max(max1,pre[1][i][x]),min(min1,pre[0][i][x]),max(max2,suf[1][i][x+1]),min(min2,suf[0][i][x+1]));
}
}
void dfs2(int i,int j,int max1,int min1,int max2,int min2){
//cout<<"i="<<i<<" j="<<j<<endl;
//cout<<max1<<" "<<min1<<" "<<max2<<" "<<min2<<endl;
if(i==h+1){
if(j==w) return;
ans=min(ans,max(max1-min1,max2-min2)); return;
}
REP(x,j,0){
dfs2(i+1,x,max(max1,pre[1][i][x]),min(min1,pre[0][i][x]),max(max2,suf[1][i][x+1]),min(min2,suf[0][i][x+1]));
}
}
void turn(int t){
FOR(i,1,w+1){
FOR(j,1,h+1){
a[t][i][j]=a[t^1][j][w-i+1];
}
}
swap(h,w);
}
void flap(int t){
FOR(i,1,h+1){
FOR(j,1,w+1){
a[t][i][j]=a[t^1][h-i+1][w-j+1];
}
}
}
int now=0;
int minn=INF;
bool check(int k){
//cout<<"k="<<k<<endl;
FOR(i,1,8){
bool ok=1;
if(i==4){
flap(now^1); now^=1;
}
FOR(i,1,h+1){
pre[1][i][0]=0;
suf[1][i][w+1]=0;
pre[0][i][0]=INF;
suf[0][i][w+1]=INF;
FOR(j,1,w+1){
pre[0][i][j]=min(pre[0][i][j-1],a[now][i][j]);
pre[1][i][j]=max(pre[1][i][j-1],a[now][i][j]);
//cout<<"pre["<<i<<"]["<<j<<"]="<<pre[1][i][j]<<endl;
}
REP(j,w,1){
suf[0][i][j]=min(suf[0][i][j+1],a[now][i][j]);
suf[1][i][j]=max(suf[1][i][j+1],a[now][i][j]);
//cout<<"suf["<<i<<"]["<<j<<"]="<<suf[1][i][j]<<endl;
}
}
int tmp=w,mi=INF,mx=0,lm=INF;
FOR(x,1,h+1){
while(tmp>0&&pre[1][x][tmp]>minn+k){
tmp--;
}
lm=min(lm,pre[0][x][tmp]);
mi=min(mi,suf[0][x][tmp+1]);
mx=max(mx,suf[1][x][tmp+1]);
//cout<<"mx="<<mx<<" mi="<<mi<<" lm="<<lm<<" tmp="<<tmp<<endl;
if(mx-mi>k){
ok=0; break;
}
}
if(lm!=minn){
ok=0;
}
if(ok){
return 1;
}
turn(now^1); now^=1;
}
return 0;
}
signed main(){
AC;
cin>>h>>w;
FOR(i,1,h+1)FOR(j,1,w+1){
cin>>a[0][i][j]; minn=min(minn,a[0][i][j]);
}
int l=0,r=1e9+1,ans=INF;
while(l<r){
int mid=(l+r)>>1;
if(check(mid)){
r=mid; ans=min(ans,mid);
}
else{
l=mid+1;
}
}
cout<<ans<<endl;
}
/*
4 5
1 2 4 4
1 3 2 1
4 3 1 2
4 1 6 1
2 4 2 5
*/
Compilation message
joioi.cpp: In function 'void file()':
joioi.cpp:44:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
44 | freopen("/Users/liaoyunyang/Desktop/meta_in.txt","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:45:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
45 | freopen("/Users/liaoyunyang/Desktop/meta_out.txt","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
11 ms |
6996 KB |
Output is correct |
17 |
Correct |
22 ms |
7068 KB |
Output is correct |
18 |
Correct |
30 ms |
6992 KB |
Output is correct |
19 |
Correct |
24 ms |
6996 KB |
Output is correct |
20 |
Correct |
33 ms |
6952 KB |
Output is correct |
21 |
Correct |
41 ms |
7044 KB |
Output is correct |
22 |
Correct |
42 ms |
7056 KB |
Output is correct |
23 |
Correct |
27 ms |
7060 KB |
Output is correct |
24 |
Correct |
36 ms |
6996 KB |
Output is correct |
25 |
Correct |
31 ms |
7040 KB |
Output is correct |
26 |
Correct |
32 ms |
6996 KB |
Output is correct |
27 |
Correct |
41 ms |
7044 KB |
Output is correct |
28 |
Correct |
45 ms |
7048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
468 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
1 ms |
468 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
3 ms |
5332 KB |
Output is correct |
16 |
Correct |
11 ms |
6996 KB |
Output is correct |
17 |
Correct |
22 ms |
7068 KB |
Output is correct |
18 |
Correct |
30 ms |
6992 KB |
Output is correct |
19 |
Correct |
24 ms |
6996 KB |
Output is correct |
20 |
Correct |
33 ms |
6952 KB |
Output is correct |
21 |
Correct |
41 ms |
7044 KB |
Output is correct |
22 |
Correct |
42 ms |
7056 KB |
Output is correct |
23 |
Correct |
27 ms |
7060 KB |
Output is correct |
24 |
Correct |
36 ms |
6996 KB |
Output is correct |
25 |
Correct |
31 ms |
7040 KB |
Output is correct |
26 |
Correct |
32 ms |
6996 KB |
Output is correct |
27 |
Correct |
41 ms |
7044 KB |
Output is correct |
28 |
Correct |
45 ms |
7048 KB |
Output is correct |
29 |
Execution timed out |
4078 ms |
188636 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |