#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;
pii mx[K];
int pref[K][N][N], suff_max[K][N][N], suff_min[K][N][N];
vector<vector<vector<int> > > v;
void precalc(int id) {
// global max
vector<vector<int> > a = v[id];
int cur_n = sz(a);
int cur_m = sz(a[0]);
for (int i = 0; i < cur_n; i++) {
for (int j = 0; j < cur_m; j++) {
if (a[mx[id].f][mx[id].s] < a[i][j]) {
mx[id] = mp(i, j);
}
}
}
for (int i = 0; i < cur_n; i++) {
for (int j = 0; j < cur_m; j++) {
pref[id][i][j] = a[i][j];
if (j > 0)
pref[id][i][j] = min(pref[id][i][j], pref[id][i][j - 1]);
}
suff_min[id][i][cur_m] = INF;
suff_max[id][i][cur_m] = 0;
for (int j = cur_m - 1; j >= 0; j--) {
suff_max[id][i][j] = max(a[i][j], suff_max[id][i][j + 1]);
suff_min[id][i][j] = min(a[i][j], suff_min[id][i][j + 1]);
}
}
}
bool solve(int id, int diff) {
int cur_n = sz(v[id]);
int cur_m = sz(v[id][0]);
int ptr = cur_m - 1;
int max_val = v[id][mx[id].f][mx[id].s];
int mn = INF, mx = 0;
for (int i = 0; i < cur_n; i++) {
while (ptr >= 0 && max_val > diff + pref[id][i][ptr]) {
ptr--;
}
mn = min(mn, suff_min[id][i][ptr + 1]);
mx = max(mx, suff_max[id][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]);
}
}
}
int b[N][N];
void rotate() {
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];
}
}
}
bool ok(int diff) {
for (int i = 0; i < sz(v); i++) {
if (solve(i, diff))
return true;
}
return false;
}
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]);
}
}
for (int i = 0; i < 4; i++) {
v.pb(a);
mirror();
v.pb(a);
mirror();
rotate();
}
for (int i = 0; i < sz(v); i++) {
precalc(i);
}
int l = 1, r = INF;
int ans;
while (l <= r) {
int mid = (l + r) / 2;
if (ok(mid)) {
r = mid - 1;
ans = mid;
} else {
l = mid + 1;
}
}
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:117:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &a[i][j]);
~~~~~^~~~~~~~~~~~~~~~
joioi.cpp:141:7: warning: 'ans' may be used uninitialized in this function [-Wmaybe-uninitialized]
cout << ans;
~~~~~^~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1672 KB |
Output is correct |
4 |
Correct |
4 ms |
1672 KB |
Output is correct |
5 |
Correct |
4 ms |
1680 KB |
Output is correct |
6 |
Correct |
4 ms |
1680 KB |
Output is correct |
7 |
Correct |
3 ms |
1680 KB |
Output is correct |
8 |
Correct |
4 ms |
1680 KB |
Output is correct |
9 |
Correct |
3 ms |
1680 KB |
Output is correct |
10 |
Correct |
3 ms |
1680 KB |
Output is correct |
11 |
Correct |
4 ms |
1680 KB |
Output is correct |
12 |
Correct |
4 ms |
1680 KB |
Output is correct |
13 |
Correct |
3 ms |
1680 KB |
Output is correct |
14 |
Correct |
3 ms |
1812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1672 KB |
Output is correct |
4 |
Correct |
4 ms |
1672 KB |
Output is correct |
5 |
Correct |
4 ms |
1680 KB |
Output is correct |
6 |
Correct |
4 ms |
1680 KB |
Output is correct |
7 |
Correct |
3 ms |
1680 KB |
Output is correct |
8 |
Correct |
4 ms |
1680 KB |
Output is correct |
9 |
Correct |
3 ms |
1680 KB |
Output is correct |
10 |
Correct |
3 ms |
1680 KB |
Output is correct |
11 |
Correct |
4 ms |
1680 KB |
Output is correct |
12 |
Correct |
4 ms |
1680 KB |
Output is correct |
13 |
Correct |
3 ms |
1680 KB |
Output is correct |
14 |
Correct |
3 ms |
1812 KB |
Output is correct |
15 |
Correct |
10 ms |
12144 KB |
Output is correct |
16 |
Correct |
27 ms |
25340 KB |
Output is correct |
17 |
Correct |
29 ms |
26484 KB |
Output is correct |
18 |
Correct |
29 ms |
26484 KB |
Output is correct |
19 |
Correct |
31 ms |
26488 KB |
Output is correct |
20 |
Correct |
38 ms |
26488 KB |
Output is correct |
21 |
Correct |
31 ms |
26488 KB |
Output is correct |
22 |
Correct |
30 ms |
26488 KB |
Output is correct |
23 |
Correct |
32 ms |
26488 KB |
Output is correct |
24 |
Correct |
28 ms |
26488 KB |
Output is correct |
25 |
Correct |
32 ms |
26488 KB |
Output is correct |
26 |
Correct |
34 ms |
26488 KB |
Output is correct |
27 |
Correct |
32 ms |
26552 KB |
Output is correct |
28 |
Correct |
37 ms |
26552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1144 KB |
Output is correct |
2 |
Correct |
3 ms |
1144 KB |
Output is correct |
3 |
Correct |
3 ms |
1672 KB |
Output is correct |
4 |
Correct |
4 ms |
1672 KB |
Output is correct |
5 |
Correct |
4 ms |
1680 KB |
Output is correct |
6 |
Correct |
4 ms |
1680 KB |
Output is correct |
7 |
Correct |
3 ms |
1680 KB |
Output is correct |
8 |
Correct |
4 ms |
1680 KB |
Output is correct |
9 |
Correct |
3 ms |
1680 KB |
Output is correct |
10 |
Correct |
3 ms |
1680 KB |
Output is correct |
11 |
Correct |
4 ms |
1680 KB |
Output is correct |
12 |
Correct |
4 ms |
1680 KB |
Output is correct |
13 |
Correct |
3 ms |
1680 KB |
Output is correct |
14 |
Correct |
3 ms |
1812 KB |
Output is correct |
15 |
Correct |
10 ms |
12144 KB |
Output is correct |
16 |
Correct |
27 ms |
25340 KB |
Output is correct |
17 |
Correct |
29 ms |
26484 KB |
Output is correct |
18 |
Correct |
29 ms |
26484 KB |
Output is correct |
19 |
Correct |
31 ms |
26488 KB |
Output is correct |
20 |
Correct |
38 ms |
26488 KB |
Output is correct |
21 |
Correct |
31 ms |
26488 KB |
Output is correct |
22 |
Correct |
30 ms |
26488 KB |
Output is correct |
23 |
Correct |
32 ms |
26488 KB |
Output is correct |
24 |
Correct |
28 ms |
26488 KB |
Output is correct |
25 |
Correct |
32 ms |
26488 KB |
Output is correct |
26 |
Correct |
34 ms |
26488 KB |
Output is correct |
27 |
Correct |
32 ms |
26552 KB |
Output is correct |
28 |
Correct |
37 ms |
26552 KB |
Output is correct |
29 |
Runtime error |
1535 ms |
263168 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
30 |
Halted |
0 ms |
0 KB |
- |