#include<bits/stdc++.h>
using namespace std;
using ll = long long;
void file() {
const string FILE_NAME = "FILE_NAME";
freopen((FILE_NAME + ".inp").c_str(), "r", stdin);
freopen((FILE_NAME + ".out").c_str(), "w", stdout);
}
const int N = 2000 + 5;
int a[N][N];
int n, m, ma, mi;
int top_left(int x) {
for (int i = 1, cur_r = m; i <= n; ++i) {
for (int j = cur_r; j; --j)
if (ma - a[i][j] > x) cur_r = j - 1;
for (int j = cur_r + 1; j <= m; ++j)
if (a[i][j] - mi > x) return 0;
}
return 1;
}
int bot_left(int x) {
for (int i = n, cur_r = m; i; --i) {
for (int j = cur_r; j; --j)
if (ma - a[i][j] > x) cur_r = j - 1;
for (int j = cur_r + 1; j <= m; ++j)
if (a[i][j] - mi > x) return 0;
}
return 1;
}
int top_right(int x) {
for (int i = 1, cur_r = m; i <= n; ++i) {
for (int j = cur_r; j; --j)
if (a[i][j] - mi > x) cur_r = j - 1;
for (int j = cur_r + 1; j <= m; ++j)
if (ma - a[i][j] > x) return 0;
}
return 1;
}
int bot_right(int x) {
for (int i = n, cur_r = m; i; --i) {
for (int j = cur_r; j; --j)
if (a[i][j] - mi > x) cur_r = j - 1;
for (int j = cur_r + 1; j <= m; ++j)
if (ma - a[i][j] > x) return 0;
}
return 1;
}
int main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
// partition to 2 groups, each have its it_max - it_min <= x
// <=> partition to 2 groups, one have all_max - it_min <= x, one have it_max - all_min <= x
// because it_max <= all_max, it_min >= all_min
cin >> n >> m;
ma = 0, mi = 1e9;
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
cin >> a[i][j], ma = max(ma, a[i][j]), mi = min(mi, a[i][j]);
int lo = 0, hi = ma - mi, mid, ans = 0;
while (lo <= hi) {
mid = (lo + hi) >> 1;
if (top_right(mid) || top_left(mid) || bot_right(mid) || bot_left(mid))
ans = mid,
hi = mid - 1;
else
lo = mid + 1;
}
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'void file()':
joioi.cpp:6:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
6 | freopen((FILE_NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:7:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
7 | freopen((FILE_NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1492 KB |
Output is correct |
18 |
Correct |
5 ms |
1492 KB |
Output is correct |
19 |
Correct |
5 ms |
1488 KB |
Output is correct |
20 |
Correct |
4 ms |
1356 KB |
Output is correct |
21 |
Correct |
9 ms |
1620 KB |
Output is correct |
22 |
Correct |
5 ms |
1612 KB |
Output is correct |
23 |
Correct |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
5 ms |
1496 KB |
Output is correct |
25 |
Correct |
7 ms |
1620 KB |
Output is correct |
26 |
Correct |
8 ms |
1616 KB |
Output is correct |
27 |
Correct |
7 ms |
1620 KB |
Output is correct |
28 |
Correct |
8 ms |
1612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
320 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
3 ms |
1280 KB |
Output is correct |
17 |
Correct |
4 ms |
1492 KB |
Output is correct |
18 |
Correct |
5 ms |
1492 KB |
Output is correct |
19 |
Correct |
5 ms |
1488 KB |
Output is correct |
20 |
Correct |
4 ms |
1356 KB |
Output is correct |
21 |
Correct |
9 ms |
1620 KB |
Output is correct |
22 |
Correct |
5 ms |
1612 KB |
Output is correct |
23 |
Correct |
7 ms |
1620 KB |
Output is correct |
24 |
Correct |
5 ms |
1496 KB |
Output is correct |
25 |
Correct |
7 ms |
1620 KB |
Output is correct |
26 |
Correct |
8 ms |
1616 KB |
Output is correct |
27 |
Correct |
7 ms |
1620 KB |
Output is correct |
28 |
Correct |
8 ms |
1612 KB |
Output is correct |
29 |
Correct |
324 ms |
37196 KB |
Output is correct |
30 |
Correct |
313 ms |
37400 KB |
Output is correct |
31 |
Correct |
378 ms |
38996 KB |
Output is correct |
32 |
Correct |
370 ms |
39092 KB |
Output is correct |
33 |
Correct |
289 ms |
33976 KB |
Output is correct |
34 |
Correct |
328 ms |
39128 KB |
Output is correct |
35 |
Correct |
512 ms |
54648 KB |
Output is correct |
36 |
Correct |
465 ms |
49476 KB |
Output is correct |
37 |
Correct |
516 ms |
54732 KB |
Output is correct |