#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#define pb emplace_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
using namespace std;
const int N = int(2e3) + 3;
const int inf = int(1e9) + 3;
int n, m, a[N][N], Max[N][N], Min[N][N];
int low, mid, high, lim, mn, mx, res, Mx, Mn, h;
bool divided;
bool Check() {
lim = mn + mid;
if(a[0][0] > lim && a[0][m - 1] > lim) return 0;
Mx = 0, Mn = inf, h = n;
for(int j = 0; j < m; ++j) {
divided = 0;
for(int i = 0; i < n; ++i) {
if(a[i][j] <= lim && i < h && !divided) continue;
if(!divided) {
divided = 1, h = min(h, i);
Mx = max(Mx, Max[i][j]);
Mn = min(Mn, Min[i][j]);
break;
}
}
}
if(Mx - Mn <= mid) return 1;
h = n, Mx = 0, Mn = inf;
for(int j = m - 1; j >= 0; --j) {
divided = 0;
for(int i = 0; i < n; ++i) {
if(a[i][j] <= lim && i < h && !divided) continue;
if(!divided) {
divided = 1, h = min(h, i);
Mx = max(Mx, Max[i][j]);
Mn = min(Mn, Min[i][j]);
break;
}
}
}
return Mx - Mn <= mid;
}
void Solve() {
for(int i = n - 1; i >= 0; --i) {
for(int j = 0; j < m; ++j) {
if(i + 1 < n) {
Max[i][j] = max(Max[i + 1][j], a[i][j]);
Min[i][j] = min(Min[i + 1][j], a[i][j]);
} else Max[i][j] = Min[i][j] = a[i][j];
}
}
mid = res - 1;
if(!Check()) return;
low = 0, high = res - 1;
while(low <= high) {
mid = (low + high) >> 1;
if(Check()) high = mid - 1;
else low = mid + 1;
}
res = min(res, low);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
if(fopen("test.inp", "r")) {
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
}
cin >> n >> m; mn = inf, mx = 0;
for(int i = 0; i < n; ++i) {
for(int j = 0; j < m; ++j) {
cin >> a[i][j];
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
res = mx - mn;
Solve();
for(int j = 0; j < m; ++j) {
for(int i = 0; n - 1 - i > i; ++i) {
swap(a[i][j], a[n - 1 - i][j]);
}
}
Solve();
cout << res;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:75:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.inp", "r", stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:76:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen("test.out", "w", stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
400 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
400 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
7 ms |
3192 KB |
Output is correct |
17 |
Correct |
10 ms |
3192 KB |
Output is correct |
18 |
Correct |
9 ms |
3192 KB |
Output is correct |
19 |
Correct |
10 ms |
3164 KB |
Output is correct |
20 |
Correct |
9 ms |
2936 KB |
Output is correct |
21 |
Correct |
11 ms |
3192 KB |
Output is correct |
22 |
Correct |
11 ms |
3196 KB |
Output is correct |
23 |
Correct |
12 ms |
3192 KB |
Output is correct |
24 |
Correct |
11 ms |
2936 KB |
Output is correct |
25 |
Correct |
11 ms |
3192 KB |
Output is correct |
26 |
Correct |
12 ms |
3192 KB |
Output is correct |
27 |
Correct |
12 ms |
3192 KB |
Output is correct |
28 |
Correct |
12 ms |
3192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
400 KB |
Output is correct |
2 |
Correct |
2 ms |
504 KB |
Output is correct |
3 |
Correct |
2 ms |
504 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
504 KB |
Output is correct |
6 |
Correct |
2 ms |
504 KB |
Output is correct |
7 |
Correct |
3 ms |
504 KB |
Output is correct |
8 |
Correct |
2 ms |
504 KB |
Output is correct |
9 |
Correct |
2 ms |
504 KB |
Output is correct |
10 |
Correct |
2 ms |
508 KB |
Output is correct |
11 |
Correct |
2 ms |
504 KB |
Output is correct |
12 |
Correct |
2 ms |
504 KB |
Output is correct |
13 |
Correct |
2 ms |
504 KB |
Output is correct |
14 |
Correct |
2 ms |
504 KB |
Output is correct |
15 |
Correct |
3 ms |
504 KB |
Output is correct |
16 |
Correct |
7 ms |
3192 KB |
Output is correct |
17 |
Correct |
10 ms |
3192 KB |
Output is correct |
18 |
Correct |
9 ms |
3192 KB |
Output is correct |
19 |
Correct |
10 ms |
3164 KB |
Output is correct |
20 |
Correct |
9 ms |
2936 KB |
Output is correct |
21 |
Correct |
11 ms |
3192 KB |
Output is correct |
22 |
Correct |
11 ms |
3196 KB |
Output is correct |
23 |
Correct |
12 ms |
3192 KB |
Output is correct |
24 |
Correct |
11 ms |
2936 KB |
Output is correct |
25 |
Correct |
11 ms |
3192 KB |
Output is correct |
26 |
Correct |
12 ms |
3192 KB |
Output is correct |
27 |
Correct |
12 ms |
3192 KB |
Output is correct |
28 |
Correct |
12 ms |
3192 KB |
Output is correct |
29 |
Correct |
538 ms |
45096 KB |
Output is correct |
30 |
Correct |
571 ms |
47532 KB |
Output is correct |
31 |
Correct |
726 ms |
47352 KB |
Output is correct |
32 |
Correct |
589 ms |
47520 KB |
Output is correct |
33 |
Correct |
495 ms |
41608 KB |
Output is correct |
34 |
Correct |
598 ms |
47408 KB |
Output is correct |
35 |
Correct |
1184 ms |
47480 KB |
Output is correct |
36 |
Correct |
783 ms |
47324 KB |
Output is correct |
37 |
Correct |
1229 ms |
47440 KB |
Output is correct |