This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define debug(x) cout << '[' << #x << " is: " << x << "] " << endl;
#define inF freopen("6in.txt" , "r" , stdin);
#define outF freopen("6.txt" , "w" , stdout);
#define imod(a , n) (a % n + n ) % n
#define sor(v) sort(v.begin() , v.end());
#define print(v) for(auto f : v ) cout << f << " " ;
#define rsor(v) sort(v.rbegin() , v.rend());
#define rev(v) reverse(v.begin() , v.end());
#define scan(v)for(auto &it : v)cin >> it ;
#define ll long long
#define int ll
#define logar(x , y) log(x) / log(y)
#define __sum(n) n * (n + 1) / 2
#define __lcm(a , b) a / __gcd(a , b) * b
#define pii pair<int , int >
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
const int N = 2e3 + 5 , M = N * 64 , MX = 2e4 + 40;
const ll MOD = 1e9 + 7 , oo = 1e9 + 9 , OO = 1e18 , mod = MOD ;
const double pi = acos(-1) , eps = 1e-17 ;
int di[] = {1 , -1 , 0 , 0};
int dj[] = {0 , 0 , -1 , 1};
set<int> st ;
vector<int> v ;
int n , m ;
int arr[N][N];
int sz ;
bool check(int number){
int mx = 0 , mn = 1e9 , en = n;
for(int c = 0 ; c < m ; c ++){
for(int r = 0 ; r < n ; r ++){
if(arr[r][c] > number)en = min(r , en) ;
if(r >= en)mx = max(mx , arr[r][c] ) , mn = min(mn , arr[r][c]);
}
}
return mx-mn <= number - v[0];
}
bool check2(int number){
int mx = 0 , mn = 1e9 , en = n;
for(int c = 0 ; c < m ; c ++){
for(int r = 0 ; r < n ; r ++){
if(arr[r][c] < number)en = min(r , en) ;
if(r >= en)mx = max(mx , arr[r][c] ) , mn = min(mn , arr[r][c]);
}
// if(number == 10)debug(arr[0][0])
}
return mx-mn <= v.back() - number ;
}
int arr2[N][N];
int32_t main()
{ //inF;
//inF;outF;
fastio;
cin >> n >> m ;
for(int i = 0 ; i < n; i ++){
for(int j = 0 ; j <m; j ++){
cin >> arr[i][j];
arr2[i][j] = arr[i][j];
st.insert(arr[i][j]);
}
}
for(auto f : st)v.push_back(f);
sz = v.size();
ll mnans = 1e10;
for(int all = 0 ; all < 2 ; all ++){
int l = 0 , r = sz - 1 , md ;
int ans ;
while(l <= r){
md = (l + r) / 2 ;
if(check(v[md]))ans = md , r = md - 1 ;
else l = md + 1 ;
}
for(int i = ans ; i < sz ; i ++){
mnans = min(mnans , max(v[i] - v[0] , v.back() - (i + 1 == sz ? v[i] : v[i + 1])));
}
//debug(mnans);
l = 0 , r = sz - 1 , md ;
while(l <= r){
md = (l + r) / 2 ;
// if(all == 0)break;
if(check2(v[md]))ans = md , l = md + 1 ;
else r = md - 1 ;
}
// debug(ans)
for(int i = ans ; i >-1 ; i --){
mnans = min(mnans , max((i == 0 ? v[i] : v[i - 1]) - v[0] , v.back() - v[i]));
}
//debug(mnans)
for(int c = 0 ; c < m ; c ++){
for(int r = 0 ; r< n ; r ++){
arr[r][c] = arr2[r][m - 1 - c];
// debug(arr[r][c])
}
}
}
cout << mnans ;
}
Compilation message (stderr)
joioi.cpp: In function 'int32_t main()':
joioi.cpp:136:34: warning: right operand of comma operator has no effect [-Wunused-value]
136 | l = 0 , r = sz - 1 , md ;
| ^
joioi.cpp:155:87: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
155 | mnans = min(mnans , max((i == 0 ? v[i] : v[i - 1]) - v[0] , v.back() - v[i]));
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |