This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// #pragma GCC optimize("O3,Ofast,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define MP make_pair
#define pb push_back
#define REP(i,n) for(int i = 0; (i) < (n); (i)++)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void fastio() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const double EPS = 0.00001;
const int INF = 2000000000;
const int N = 3e5+5;
const int ALPH = 26;
const int LGN = 25;
constexpr int MOD = 1e9+7;
int H, W;
int mx = 0, mn = INF;
vector<vector<int> > pmx, pmn;
int sol(vector<vector<int> > &gr) {
pmx.assign(H + 3, vector<int>(W + 3, 0));
pmn.assign(H + 3, vector<int>(W + 3, INF));
for(int j = 1; j <= W; j++) {
for(int i = 1; i <= H; i++) {
pmx[i][j] = max(pmx[i - 1][j] , gr[i][j]);
}
for(int i = H; i>=1; i--) {
pmn[i][j] = min(pmn[i + 1][j] , gr[i][j]);
}
}
for(int i = 1; i <= H; i++) {
for(int j = W; j >= 1; j--) {
pmx[i][j] = max(pmx[i][j], pmx[i][j + 1]);
}
for(int j = 1; j <= W; j++) {
pmn[i][j] = min(pmn[i][j], pmn[i][j - 1]);
}
}
// for(int i = 1; i<= H; i++) {
// for(int j = 1; j <= W; j++) {
// cout << pmx[i][j] << " ";
// }
// cout << "\n";
// }
// cout << "\n";
// for(int i = 1; i<= H; i++) {
// for(int j = 1; j <= W; j++) {
// cout << pmn[i][j] << " ";
// }
// cout << "\n";
// }
int ans = 0;
int ind = 1;
for(int j = 1; j <= W; j++) {
int cur = INF, bst = ind;
for(int i = ind; i<= H + 1; i++) {
if(max(pmx[i - 1][j] - mn, mx - pmn[i][j]) < cur) {
cur = max(pmx[i - 1][j] - mn, mx - pmn[i][j]);
bst = i;
}
}
// cout << "cur:" << cur << "bst:" << bst << "\n";
ans = max(ans, cur);
ind = bst;
}
return ans;
}
inline void solve() {
cin >> H >> W;
vector<vector<int> > gr1(H + 3, vector<int>(W + 3, 0));
vector<vector<int> > gr2(H + 3, vector<int>(W + 3, 0));
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
cin >> gr1[i][j];
mn = min(mn, gr1[i][j]);
mx = max(mx, gr1[i][j]);
gr2[i][W - j + 1] = gr1[i][j];
}
}
// cout << mn << " mx:" << mx << "\n";
int ans = sol(gr1);
// cout << "ans:" << ans << "\n";
ans = min(ans, sol(gr2));
vector<vector<int> > tmp(H + 3, vector<int>(W + 3, 0));
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
tmp[H - i + 1][j] = gr1[i][j];
}
}
ans = min(ans, sol(tmp));
// cout << "ans:" << ans << "\n";
for(int i = 1; i<=H; i++) {
for(int j = 1; j <= W; j++) {
tmp[H - i + 1][j] = gr2[i][j];
}
}
ans = min(ans, sol(tmp));
cout << ans << "\n";
}
signed main() {
fastio();
int test = 1;
//cin>>test;
while(test--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |