#include <bits/stdc++.h>
#define pb push_back
#define all(v) v.begin(), v.end()
#define sz(v) int(v.size())
#define pii pair<int, int>
#define mp make_pair
#define f first
#define ll long long
#define ld long double
#define s second
#define vec vector<int>
using namespace std;
const int N = (int) 2e3 + 100;
const int M = (int) 2018 * 2018;
const int K = (int) 10;
const int INF = (int) 1e9 + 7;
const int mod = (int) 998244353;
const ld EPS = (ld) 1e-9;
const ll LINF = (ll) 1e18;
int n, m;
vector<vector<int> > a;
int mx;
int pref[N][N], suff_max[N][N], suff_min[N][N];
void precalc() {
// global max
int cur_n = sz(a);
int cur_m = sz(a[0]);
mx = 0;
for (int i = 0; i < cur_n; i++) {
for (int j = 0; j < cur_m; j++) {
mx = max(mx, a[i][j]);
}
}
for (int i = 0; i < cur_n; i++) {
for (int j = 0; j < cur_m; j++) {
pref[i][j] = a[i][j];
if (j > 0)
pref[i][j] = min(pref[i][j], pref[i][j - 1]);
}
suff_min[i][cur_m] = INF;
suff_max[i][cur_m] = 0;
for (int j = cur_m - 1; j >= 0; j--) {
suff_max[i][j] = max(a[i][j], suff_max[i][j + 1]);
suff_min[i][j] = min(a[i][j], suff_min[i][j + 1]);
}
}
}
bool ok(int diff) {
int cur_n = sz(a);
int cur_m = sz(a[0]);
int ptr = cur_m - 1;
int max_val = mx;
int mn = INF, mx = 0;
for (int i = 0; i < cur_n; i++) {
while (ptr >= 0 && max_val > diff + pref[i][ptr]) {
ptr--;
}
mn = min(mn, suff_min[i][ptr + 1]);
mx = max(mx, suff_max[i][ptr + 1]);
}
return mx - mn <= diff;
}
void mirror() {
for (int i = 0; i < n / 2; i++) {
for (int j = 0; j < m; j++) {
swap(a[i][j], a[n - 1 - i][j]);
}
}
}
void rotate() {
int b[N][N];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
b[j][n - i - 1] = a[i][j];
}
}
swap(n, m);
a.clear();
for (int i = 0; i < n; i++) {
a.pb(vector<int>(m));
for (int j = 0; j < m; j++) {
a[i][j] = b[i][j];
}
}
}
int main() {
#ifdef sony
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
srand(time(0));
cin >> n >> m;
for (int i = 0; i < n; i++) {
a.pb(vector<int>(m));
for (int j = 0; j < m; j++) {
scanf("%d", &a[i][j]);
}
}
int ans = INF;
for (int i = 0; i < 4; i++) {
precalc();
int l = 1, r = ans - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (ok(mid)) {
r = mid - 1;
ans = mid;
} else {
l = mid + 1;
}
}
mirror();
precalc();
l = 1, r = ans - 1;
while (l <= r) {
int mid = (l + r) / 2;
if (ok(mid)) {
r = mid - 1;
ans = mid;
} else {
l = mid + 1;
}
}
mirror();
rotate();
}
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:105:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
2 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
3 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
2 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
3 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
6 ms |
3820 KB |
Output is correct |
16 |
Correct |
12 ms |
4588 KB |
Output is correct |
17 |
Correct |
14 ms |
4588 KB |
Output is correct |
18 |
Correct |
14 ms |
4588 KB |
Output is correct |
19 |
Correct |
21 ms |
4588 KB |
Output is correct |
20 |
Correct |
16 ms |
4716 KB |
Output is correct |
21 |
Correct |
16 ms |
4716 KB |
Output is correct |
22 |
Correct |
16 ms |
4716 KB |
Output is correct |
23 |
Correct |
16 ms |
4716 KB |
Output is correct |
24 |
Correct |
14 ms |
4716 KB |
Output is correct |
25 |
Correct |
16 ms |
4716 KB |
Output is correct |
26 |
Correct |
16 ms |
4716 KB |
Output is correct |
27 |
Correct |
16 ms |
4716 KB |
Output is correct |
28 |
Correct |
22 ms |
4716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
656 KB |
Output is correct |
3 |
Correct |
2 ms |
656 KB |
Output is correct |
4 |
Correct |
2 ms |
656 KB |
Output is correct |
5 |
Correct |
2 ms |
656 KB |
Output is correct |
6 |
Correct |
2 ms |
656 KB |
Output is correct |
7 |
Correct |
3 ms |
832 KB |
Output is correct |
8 |
Correct |
2 ms |
832 KB |
Output is correct |
9 |
Correct |
2 ms |
832 KB |
Output is correct |
10 |
Correct |
2 ms |
832 KB |
Output is correct |
11 |
Correct |
2 ms |
832 KB |
Output is correct |
12 |
Correct |
3 ms |
832 KB |
Output is correct |
13 |
Correct |
2 ms |
832 KB |
Output is correct |
14 |
Correct |
2 ms |
832 KB |
Output is correct |
15 |
Correct |
6 ms |
3820 KB |
Output is correct |
16 |
Correct |
12 ms |
4588 KB |
Output is correct |
17 |
Correct |
14 ms |
4588 KB |
Output is correct |
18 |
Correct |
14 ms |
4588 KB |
Output is correct |
19 |
Correct |
21 ms |
4588 KB |
Output is correct |
20 |
Correct |
16 ms |
4716 KB |
Output is correct |
21 |
Correct |
16 ms |
4716 KB |
Output is correct |
22 |
Correct |
16 ms |
4716 KB |
Output is correct |
23 |
Correct |
16 ms |
4716 KB |
Output is correct |
24 |
Correct |
14 ms |
4716 KB |
Output is correct |
25 |
Correct |
16 ms |
4716 KB |
Output is correct |
26 |
Correct |
16 ms |
4716 KB |
Output is correct |
27 |
Correct |
16 ms |
4716 KB |
Output is correct |
28 |
Correct |
22 ms |
4716 KB |
Output is correct |
29 |
Correct |
1076 ms |
81484 KB |
Output is correct |
30 |
Correct |
1026 ms |
81484 KB |
Output is correct |
31 |
Correct |
1096 ms |
82324 KB |
Output is correct |
32 |
Correct |
1118 ms |
82628 KB |
Output is correct |
33 |
Correct |
993 ms |
82628 KB |
Output is correct |
34 |
Correct |
1056 ms |
82628 KB |
Output is correct |
35 |
Correct |
1242 ms |
82640 KB |
Output is correct |
36 |
Correct |
1074 ms |
82640 KB |
Output is correct |
37 |
Correct |
1327 ms |
121296 KB |
Output is correct |