#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout (T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
const int SIZE = 2005;
int n, m;
int a[SIZE][SIZE];
int mx, mn;
int u[SIZE], d[SIZE];
bool ok(int delta) {
bool f = 1;
FOR (j, 1, m) {
u[j] = 0, d[j] = n;
FOR (i, 1, n) {
if (mx - delta > a[i][j]) break;
u[j] = i;
}
for (int i = n; i >= 1; i--) {
if (mn + delta < a[i][j]) break;
d[j] = i - 1;
}
if (u[j] < d[j]) {
f = 0;
break;
}
}
if (f) {
int cur = 0;
FOR (j, 1, m) {
cur = max(cur, d[j]);
if (cur > u[j]) {
f = 0;
break;
}
}
if (f) return 1;
f = 1;
cur = n;
FOR (j, 1, m) {
cur = min(cur, u[j]);
if (cur < d[j]) {
f = 0;
break;
}
}
if (f) return 1;
}
f = 1;
FOR (j, 1, m) {
u[j] = 0, d[j] = n;
FOR (i, 1, n) {
if (mn + delta < a[i][j]) break;
u[j] = i;
}
for (int i = n; i >= 1; i--) {
if (mx - delta > a[i][j]) break;
d[j] = i - 1;
}
if (u[j] < d[j]) {
f = 0;
break;
}
}
if (f) {
int cur = 0;
FOR (j, 1, m) {
cur = max(cur, d[j]);
if (cur > u[j]) {
f = 0;
break;
}
}
if (f) return 1;
f = 1;
cur = n;
FOR (j, 1, m) {
cur = min(cur, u[j]);
if (cur < d[j]) {
f = 0;
break;
}
}
if (f) return 1;
}
return 0;
}
void solve() {
cin >> n >> m;
FOR (i, 1, n) FOR (j, 1, m) cin >> a[i][j];
mn = INT_MAX;
FOR (i, 1, n) FOR (j, 1, m) {
mx = max(mx, a[i][j]);
mn = min(mn, a[i][j]);
}
int l = 0, r = mx - mn;
while (l < r) {
int mid = (l + r) / 2;
if (ok(mid)) r = mid;
else l = mid + 1;
}
cout << l << '\n';
}
int main() {
Waimai;
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
2 ms |
2524 KB |
Output is correct |
17 |
Correct |
4 ms |
2884 KB |
Output is correct |
18 |
Correct |
3 ms |
2648 KB |
Output is correct |
19 |
Correct |
3 ms |
2652 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
6 ms |
2908 KB |
Output is correct |
22 |
Correct |
4 ms |
2908 KB |
Output is correct |
23 |
Correct |
4 ms |
2924 KB |
Output is correct |
24 |
Correct |
4 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
4 ms |
2908 KB |
Output is correct |
27 |
Correct |
5 ms |
2908 KB |
Output is correct |
28 |
Correct |
5 ms |
3008 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
464 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
2 ms |
2524 KB |
Output is correct |
17 |
Correct |
4 ms |
2884 KB |
Output is correct |
18 |
Correct |
3 ms |
2648 KB |
Output is correct |
19 |
Correct |
3 ms |
2652 KB |
Output is correct |
20 |
Correct |
3 ms |
2652 KB |
Output is correct |
21 |
Correct |
6 ms |
2908 KB |
Output is correct |
22 |
Correct |
4 ms |
2908 KB |
Output is correct |
23 |
Correct |
4 ms |
2924 KB |
Output is correct |
24 |
Correct |
4 ms |
2908 KB |
Output is correct |
25 |
Correct |
4 ms |
2908 KB |
Output is correct |
26 |
Correct |
4 ms |
2908 KB |
Output is correct |
27 |
Correct |
5 ms |
2908 KB |
Output is correct |
28 |
Correct |
5 ms |
3008 KB |
Output is correct |
29 |
Correct |
259 ms |
26492 KB |
Output is correct |
30 |
Correct |
244 ms |
27176 KB |
Output is correct |
31 |
Correct |
301 ms |
27868 KB |
Output is correct |
32 |
Correct |
247 ms |
27728 KB |
Output is correct |
33 |
Correct |
250 ms |
25168 KB |
Output is correct |
34 |
Correct |
255 ms |
27728 KB |
Output is correct |
35 |
Correct |
370 ms |
35860 KB |
Output is correct |
36 |
Correct |
370 ms |
33208 KB |
Output is correct |
37 |
Correct |
461 ms |
35864 KB |
Output is correct |