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/extc++.h"
using namespace std;
template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
cerr << t;
((cerr << " | " << u), ...);
cerr << endl;
}
#ifdef DEBUG
#define dbg(...) \
cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]: "; \
dbgh(__VA_ARGS__)
#else
#define dbg(...)
#define cerr \
if (false) \
cerr
#endif
template <typename T>
using min_pq = priority_queue<T, vector<T>, greater<>>;
#define endl "\n"
#define long int64_t
#define sz(x) int(std::size(x))
inline void init_io() {
cin.tie(nullptr);
cin.exceptions(ios::failbit);
ios_base::sync_with_stdio(false);
}
template <typename T>
T reversed(T arr) {
reverse(begin(arr), end(arr));
return arr;
}
template <typename T>
vector<vector<T>> transposed(const vector<vector<T>>& arr) {
int n = sz(arr), m = sz(arr[0]);
vector ans(m, vector<T>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[j][i] = arr[i][j];
}
}
return ans;
}
template <typename T>
ostream& operator<<(ostream& out, const vector<T>& arr) {
out << "[";
for (int i = 0; i < sz(arr); i++) {
if (i) {
out << ", ";
}
out << arr[i];
}
return out << "]";
}
inline vector<bool>::reference operator|=(vector<bool>::reference&& a, bool b) {
a = a | b;
return a;
}
vector<int> cvals[2];
int grade(const vector<vector<int>>& arr, const vector<int>& lens) {
int n = sz(arr), m = sz(arr[0]);
for (auto& a : cvals) {
a.clear();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cvals[j >= lens[i]].push_back(arr[i][j]);
}
}
int ans = 0;
for (auto& a : cvals) {
if (!sz(a)) {
continue;
}
ans = max(ans, *max_element(begin(a), end(a)) -
*min_element(begin(a), end(a)));
}
return ans;
}
vector<int> lens;
int solve1(const vector<vector<int>>& arr, int kv) {
dbg(arr, kv);
int n = sz(arr), m = sz(arr[0]);
lens.resize(n);
fill(begin(lens), end(lens), 0);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (arr[i][j] >= kv) {
lens[i] = j + 1;
}
}
}
dbg(lens);
for (int i = n - 2; i >= 0; i--) {
lens[i] = max(lens[i], lens[i + 1]);
}
return grade(arr, lens);
}
int solve1_all(vector<vector<int>> arr, int kv) {
int ans = 1e9;
ans = min(ans, solve1(arr, kv));
ans = min(ans, solve1(reversed(arr), kv));
for (auto& a : arr) {
reverse(begin(a), end(a));
}
ans = min(ans, solve1(arr, kv));
ans = min(ans, solve1(reversed(arr), kv));
return ans;
}
void solve() {
int n, m;
cin >> n >> m;
vector arr(n, vector(m, -1));
for (auto& a : arr) {
for (auto& b : a) {
cin >> b;
}
}
int mn = 1e9;
for (auto& a : arr) {
for (auto& b : a) {
mn = min(mn, b);
}
}
auto a1 = arr, a2 = reversed(arr);
for (auto& a : arr) {
reverse(begin(a), end(a));
}
auto a3 = arr, a4 = reversed(arr);
auto check = [&](int x) -> bool {
return solve1(a1, mn + x + 1) <= x || solve1(a2, mn + x + 1) <= x ||
solve1(a3, mn + x + 1) <= x || solve1(a4, mn + x + 1) <= x;
};
int l = 0, r = 1e9;
while (l < r) {
int mid = (l + r) / 2;
if (check(mid)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << endl;
}
int main() {
init_io();
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... |