#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 < 2; f1++) {
for (int f2 = 0; f2 < 2; f2++) {
mn1 = inf; mx1 = 0;
for (int _i = 1; _i <= n; _i++) {
i = _i;
if (f1) i = n - _i + 1;
dp[i] = 0; mx = 0;
for (int _j = 1; _j <= m; _j++) {
j = _j;
if (f2) j = m - _j + 1;
mx = max(mx, arr[i][j]);
if (mx - mn > mid) break;
dp[i]++;
} if (!f1) dp[i] = min(dp[i], dp[i - 1]);
else dp[i] = min(dp[i], dp[i + 1]);
for (int _j = dp[i] + 1; _j <= m; _j++) {
j = _j;
if (f2) j = m - _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; dp[n + 1] = inf;
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
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 |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
2688 KB |
Output is correct |
17 |
Correct |
9 ms |
2652 KB |
Output is correct |
18 |
Correct |
8 ms |
2784 KB |
Output is correct |
19 |
Correct |
8 ms |
2688 KB |
Output is correct |
20 |
Correct |
8 ms |
2652 KB |
Output is correct |
21 |
Correct |
10 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
2992 KB |
Output is correct |
23 |
Correct |
9 ms |
2908 KB |
Output is correct |
24 |
Correct |
8 ms |
2960 KB |
Output is correct |
25 |
Correct |
12 ms |
3012 KB |
Output is correct |
26 |
Correct |
12 ms |
2908 KB |
Output is correct |
27 |
Correct |
14 ms |
2908 KB |
Output is correct |
28 |
Correct |
11 ms |
3004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
600 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
460 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
9 ms |
2688 KB |
Output is correct |
17 |
Correct |
9 ms |
2652 KB |
Output is correct |
18 |
Correct |
8 ms |
2784 KB |
Output is correct |
19 |
Correct |
8 ms |
2688 KB |
Output is correct |
20 |
Correct |
8 ms |
2652 KB |
Output is correct |
21 |
Correct |
10 ms |
2908 KB |
Output is correct |
22 |
Correct |
9 ms |
2992 KB |
Output is correct |
23 |
Correct |
9 ms |
2908 KB |
Output is correct |
24 |
Correct |
8 ms |
2960 KB |
Output is correct |
25 |
Correct |
12 ms |
3012 KB |
Output is correct |
26 |
Correct |
12 ms |
2908 KB |
Output is correct |
27 |
Correct |
14 ms |
2908 KB |
Output is correct |
28 |
Correct |
11 ms |
3004 KB |
Output is correct |
29 |
Correct |
679 ms |
37476 KB |
Output is correct |
30 |
Correct |
665 ms |
37456 KB |
Output is correct |
31 |
Correct |
758 ms |
39284 KB |
Output is correct |
32 |
Correct |
852 ms |
39244 KB |
Output is correct |
33 |
Correct |
668 ms |
35028 KB |
Output is correct |
34 |
Correct |
851 ms |
39256 KB |
Output is correct |
35 |
Correct |
824 ms |
54908 KB |
Output is correct |
36 |
Correct |
678 ms |
49636 KB |
Output is correct |
37 |
Correct |
754 ms |
54880 KB |
Output is correct |