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;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
#define int ll
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define all(x) x.begin(), x.end()
#define sz(x) (int)x.size()
#define mispertion ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define F first
#define S second
#define getlast(s) (*s.rbegin())
#define debg cout << "OK\n"
const ld PI = 3.1415926535;
const int N = 5000+10;
const int M = 7e6 + 1;
int mod = 998244353;
const int infi = INT_MAX;
const ll infl = LLONG_MAX;
const int P = 2;
int mult(int a, int b) {
return a * 1LL * b % mod;
}
int sum(int a, int b) {
if (a + b < 0)
return a + b + mod;
if (a + b >= mod)
return a + b - mod;
return a + b;
}
ll binpow(ll a, ll n) {
if (n == 0)
return 1;
if (n % 2 == 1) {
return binpow(a, n - 1) * a % mod;
} else {
ll b = binpow(a, n / 2);
return b * b % mod;
}
}
int H, W, mat[N][N], tmp[N][N];
void rotate(){
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
tmp[W - j - 1][i] = mat[i][j];
}
}
for(int i = 0; i < W; i++)
for(int j = 0; j < H; j++)
mat[i][j] = tmp[i][j];
swap(H, W);
}
bool check(int mxd){
int ndmn = infi;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++){
ndmn = min(ndmn, mat[i][j]);
}
}
int mx = mxd + ndmn;
int cmx = W - 1;
int dmn = infi, dmx = -infi;
for(int i = 0; i < H; i++){
int ha = 0;
for(; ha <= cmx; ha++){
if(mat[i][ha] > mx)
break;
}
cmx = ha - 1;
//cout << ha << '\n';
for(int j = ha; j < W; j++){
dmn = min(dmn, mat[i][j]);
dmx = max(dmx, mat[i][j]);
}
}
//cout << '\n';
return (dmx - dmn <= mxd);
}
void solve(){
cin >> H >> W;
for(int i = 0; i < H; i++){
for(int j = 0; j < W; j++)
cin >> mat[i][j];
}
/*cout << '\n';
for(int k = 0; k < 4; k++){
for(int i = 0; i < sz(mat); i++)
for(int j = 0; j < sz(mat[i]); j++)
cout << mat[i][j] << " \n"[sz(mat[i]) - 1 == j];
rotate();
cout << '\n';
}*/
int lo = -1, hi = 1e9;
while(lo + 1 < hi){
int m = (lo + hi) / 2;
bool ok = false;
for(int i = 0; i < 4; i++){
if(check(m))
ok = true;
rotate();
}
if(ok)
hi = m;
else
lo = m;
}
cout << hi << '\n';
}
signed main() {
mispertion;
int t = 1;
//cin >> t;
while(t--){
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |