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("Ofast")
//#pragma GCC target("avx,avx2,sse,sse2,sse3,ssse3,sse4,abm,popcnt,mmx")
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
typedef double db;
typedef long double ldb;
typedef complex<double> cd;
constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
constexpr db PI = acos(-1);
constexpr bool IS_FILE = false, IS_TEST_CASES = false;
random_device rd;
mt19937 rnd32(rd());
mt19937_64 rnd64(rd());
template<typename T>
bool assign_max(T& a, T b) {
if (b > a) {
a = b;
return true;
}
return false;
}
template<typename T>
bool assign_min(T& a, T b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<typename T>
T square(T a) {
return a * a;
}
template<>
struct std::hash<pair<ll, ll>> {
ll operator() (pair<ll, ll> p) const {
return ((__int128)p.first * MOD + p.second) % INF64;
}
};
ll get(set<pair<ll, pair<ll, ll>>>& s) {
if (s.empty()) {
return 0;
}
return s.rbegin()->first - s.begin()->first;
}
ll sol(ll n, ll m, vector<vector<ll>>& arr) {
vector<tuple<ll, ll, ll>> s1;
vector<vector<ll>> dels;
ll mi = INF32, ma = 0;
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++) {
s1.emplace_back(arr[i][j], i, j);
}
}
sort(s1.rbegin(), s1.rend());
vector<ll> col(n, m - 1);
vector<vector<pair<ll, ll>>> del;
vector<ll> ans(1, 0);
for (auto[_, x, y] : s1) {
if (col[x] < y) {
continue;
}
del.emplace_back(0);
for (ll j = x; j < n; j++) {
if (col[j] < y) {
break;
}
while (col[j] >= y) {
del.back().emplace_back(j, col[j]);
assign_max(ma, arr[j][col[j]]);
assign_min(mi, arr[j][col[j]]);
col[j]--;
}
}
ans.push_back(ma - mi);
}
mi = INF32;
ma = 0;
ll na = ans.back();
while (!del.empty()) {
for (auto[x, y] : del.back()) {
assign_max(ma, arr[x][y]);
assign_min(mi, arr[x][y]);
}
del.pop_back();
ans.pop_back();
assign_min(na, max(ans.back(), ma - mi));
}
return na;
}
void solve() {
ll n, m;
cin >> n >> m;
vector<vector<ll>> arr(n, vector<ll>(m));
for (ll i = 0; i < n; i++) {
for (ll j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
ll ans = INF32;
for (ll i = 0; i < 2; i++) {
reverse(arr.begin(), arr.end());
for (ll j = 0; j < 2; j++) {
for (auto& k : arr) {
reverse(k.begin(), k.end());
}
assign_min(ans, sol(n, m, arr));
}
}
cout << ans << '\n';
}
int main() {
if (IS_FILE) {
freopen("", "r", stdin);
freopen("", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll t = 1;
if (IS_TEST_CASES) {
cin >> t;
}
for (ll i = 0; i < t; i++) {
solve();
}
}
Compilation message (stderr)
joioi.cpp:12:22: warning: overflow in conversion from 'long int' to 'll' {aka 'int'} changes value from '9000000000000000000' to '-494665728' [-Woverflow]
12 | constexpr ll INF64 = 9'000'000'000'000'000'000, INF32 = 2'000'000'000, MOD = 1'000'000'007;
| ^~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp: In function 'int main()':
joioi.cpp:127:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
127 | freopen("", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:128:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen("", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |