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>
#include <bits/extc++.h>
using namespace __gnu_pbds;
using namespace std;
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ull unsigned long long
#define all(x) (x).begin(),(x).end()
// #define int long long
// #define int unsigned long long
// #define ordered_set(T) tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>
// #define ordered_multiset(T) tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>
void open_file(string filename) {
freopen((filename + ".in").c_str(), "r", stdin);
freopen((filename + ".out").c_str(), "w", stdout);
}
// const ll mod = 1e9 + 7;
// const ll mod = 998244353;
const ll inf = 1e9;
const ll biginf = 9 * 1e18;
const int maxN = 2005;
int n, m, mn = inf, arr[maxN][maxN], dp[maxN];
bool check(int mid) {
bool f = 0;
int i, j, mx, mn1, mx1;
for (int f1 = 0; f1 < 1; f1++) {
for (int f2 = 0; f2 < 1; f2++) {
mn1 = inf; mx1 = 0;
for (int _i = 1; _i <= n; _i++) {
i = _i; mx = 0; dp[i] = 0;
if (f1) i = n - _i + 1;
for (int _j = 1; _j <= m; _j++) {
j = _j;
if (f2) j = n - _j + 1;
mx = max(mx, arr[i][j]);
if (mx - mn > mid) break;
dp[i]++;
} dp[i] = min(dp[i], dp[i - 1]);
for (int _j = dp[i] + 1; _j <= m; _j++) {
j = _j;
if (f2) j = n - _j + 1;
mn1 = min(mn1, arr[i][j]);
mx1 = max(mx1, arr[i][j]);
}
} f |= (mx1 - mn1 <= mid);
}
} return f;
}
void solve() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
cin >> arr[i][j]; mn = min(mn, arr[i][j]);
}
} int l = 1, r = inf, mid, ans = 0;
while (l <= r) {
mid = (l + r) / 2;
if (check(mid)) {
ans = mid; r = mid - 1;
} else l = mid + 1;
} cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
dp[0] = inf;
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\n';
}
return 0;
}
Compilation message (stderr)
joioi.cpp: In function 'void open_file(std::string)':
joioi.cpp:26:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
26 | freopen((filename + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:27:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen((filename + ".out").c_str(), "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... |