#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
#define mp make_pair
using namespace std;
const int MOD = 1e9+7, MAXN = 1e5+5;
const string NAME = "";
int n,m,a[2005][2005],MIN=1e9,MAX=0;
bool check1(int x){
int curMAX=0, pre=m;
pair<int,int> bottom={1e9,0};
for(int i = 1; i<=n; ++i){
for(int j = 1; j<=pre; ++j){
if(MAX-a[i][j]>x){
pre=j-1;
break;
}else curMAX=max(curMAX,a[i][j]);
}
for(int j = pre+1; j<=m; ++j)
bottom.fi=min(bottom.fi,a[i][j]), bottom.se=max(bottom.se,a[i][j]);
}
if(curMAX==MAX) return bottom.se-bottom.fi<=x;
return 0;
}
bool check2(int x){
int curMIN=1e9, pre=m;
pair<int,int> bottom={1e9,0};
for(int i = 1; i<=n; ++i){
for(int j = 1; j<=pre; ++j){
if(a[i][j]-MIN>x){
pre=j-1;
break;
}else curMIN=min(curMIN,a[i][j]);
}
for(int j = pre+1; j<=m; ++j)
bottom.fi=min(bottom.fi,a[i][j]), bottom.se=max(bottom.se,a[i][j]);
}
if(curMIN==MIN) return bottom.se-bottom.fi<=x;
return 0;
}
bool check3(int x){
int curMAX=0, pre=m;
pair<int,int> top={1e9,0};
for(int i = n; i>0; --i){
for(int j = 1; j<=pre; ++j){
if(MAX-a[i][j]>x){
pre=j-1;
break;
}else curMAX=max(curMAX,a[i][j]);
}
for(int j = pre+1; j<=m; ++j)
top.fi=min(top.fi,a[i][j]), top.se=max(top.se,a[i][j]);
}
if(curMAX==MAX) return top.se-top.fi<=x;
return 0;
}
bool check4(int x){
int curMIN=1e9, pre=m;
pair<int,int> top={1e9,0};
for(int i = n; i>0; --i){
for(int j = 1; j<=pre; ++j){
if(a[i][j]-MIN>x){
pre=j-1;
break;
}else curMIN=min(curMIN,a[i][j]);
}
for(int j = pre+1; j<=m; ++j)
top.fi=min(top.fi,a[i][j]), top.se=max(top.se,a[i][j]);
}
if(curMIN==MIN) return top.se-top.fi<=x;
return 0;
}
int bs(int l, int r){
int rs=r;
while(l<=r){
int mid=(l+r)>>1;
if(check1(mid)||check2(mid)||check3(mid)||check4(mid)) rs=mid, r=mid-1;
else l=mid+1;
}
return rs;
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> n >> m;
for(int i = 1; i<=n; ++i)
for(int j = 1; j<=m; ++j)
cin >> a[i][j], MIN=min(MIN,a[i][j]), MAX=max(MAX,a[i][j]);
if(MAX==MIN) return cout << 0, 0;
cout << bs(1,MAX-MIN);
}
컴파일 시 표준 에러 (stderr) 메시지
joioi.cpp: In function 'int main()':
joioi.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:99:45: note: in expansion of macro 'fo'
99 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
joioi.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:99:45: note: in expansion of macro 'fo'
99 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |