#include <bits/stdc++.h>
using namespace std;
#define int long long
int mn = 1e11, mx = -1e11;
bool good(int n, int m, vector<vector<int>> &a, int x) {
int pos = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] <= mx - x) pos = max(j, pos);
}
for (int j = 0; j < m; j++) {
if (a[i][j] >= mn + x && j <= pos) return 0;
}
}
return 1;
}
int solve(int n, int m, vector<vector<int>> &a) {
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout << a[i][j] << " ";
}
cout << "\n";
}*/
int l = -1, r = 2e9;
while (r - l > 1) {
int md = (l + r) / 2;
if (good(n, m, a, md)) {
//cout << "Good " << md << "\n";
r = md;
} else {
//cout << "Bad " << md << "\n";
l = md;
}
}
//cout << l << "\n\n";
//cout << "." << endl;
return l;
}
vector<vector<int>> flipv(int n, int m, vector<vector<int>> &a) {
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i][j] = a[i][m - j - 1];
}
}
return ans;
}
vector<vector<int>> flips(int n, int m, vector<vector<int>> &a) {
mn = -mn, mx = -mx;
swap(mn, mx);
vector<vector<int>> ans(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ans[i][j] = -a[i][j];
}
}
return ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int>(m));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> a[i][j];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
int ans = solve(n, m, a);
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flips(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
reverse(a.begin(), a.end());
ans = min(ans, solve(n, m, a));
cout << ans << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
14 ms |
1116 KB |
Output is correct |
17 |
Correct |
14 ms |
1368 KB |
Output is correct |
18 |
Correct |
14 ms |
1360 KB |
Output is correct |
19 |
Correct |
17 ms |
1208 KB |
Output is correct |
20 |
Correct |
14 ms |
1216 KB |
Output is correct |
21 |
Correct |
11 ms |
1456 KB |
Output is correct |
22 |
Correct |
11 ms |
1372 KB |
Output is correct |
23 |
Correct |
14 ms |
1432 KB |
Output is correct |
24 |
Correct |
10 ms |
1216 KB |
Output is correct |
25 |
Correct |
17 ms |
1492 KB |
Output is correct |
26 |
Correct |
12 ms |
1460 KB |
Output is correct |
27 |
Correct |
11 ms |
1460 KB |
Output is correct |
28 |
Correct |
11 ms |
1596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
452 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
456 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
14 ms |
1116 KB |
Output is correct |
17 |
Correct |
14 ms |
1368 KB |
Output is correct |
18 |
Correct |
14 ms |
1360 KB |
Output is correct |
19 |
Correct |
17 ms |
1208 KB |
Output is correct |
20 |
Correct |
14 ms |
1216 KB |
Output is correct |
21 |
Correct |
11 ms |
1456 KB |
Output is correct |
22 |
Correct |
11 ms |
1372 KB |
Output is correct |
23 |
Correct |
14 ms |
1432 KB |
Output is correct |
24 |
Correct |
10 ms |
1216 KB |
Output is correct |
25 |
Correct |
17 ms |
1492 KB |
Output is correct |
26 |
Correct |
12 ms |
1460 KB |
Output is correct |
27 |
Correct |
11 ms |
1460 KB |
Output is correct |
28 |
Correct |
11 ms |
1596 KB |
Output is correct |
29 |
Correct |
1342 ms |
71228 KB |
Output is correct |
30 |
Correct |
1334 ms |
80164 KB |
Output is correct |
31 |
Correct |
1548 ms |
86068 KB |
Output is correct |
32 |
Correct |
1333 ms |
86376 KB |
Output is correct |
33 |
Correct |
1149 ms |
75092 KB |
Output is correct |
34 |
Correct |
1465 ms |
86352 KB |
Output is correct |
35 |
Correct |
951 ms |
101712 KB |
Output is correct |
36 |
Correct |
838 ms |
89064 KB |
Output is correct |
37 |
Correct |
1222 ms |
101840 KB |
Output is correct |