#include <bits/stdc++.h>
using namespace std;
#define int long long
bool good(int n, int m, vector<vector<int>> &a, int x) {
int mn = 1e11, mx = -1e11;
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]);
}
}
vector<vector<int>> need(n, vector<int>(m));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] <= mx - x) need[i][j] += 1;
if (a[i][j] >= mn + x) need[i][j] += 2;
if (need[i][j] == 3) return 0;
}
}
/*for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
switch (need[i][j]) {
case 0: cout << " "; break;
case 1: cout << "- "; break;
case 2: cout << "+ "; break;
case 3: cout << "± ";
}
}
cout << "\n";
}*/
for (int i = 0; i < n; i++) {
bool ones = 1;
for (int j = 0; j < m; j++) {
if (need[i][j] == 1) {
if (!ones) return 0;
}
if (need[i][j] == 2) {
if (ones) ones = 0;
}
}
}
vector<int> depth(n, -1);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (need[i][j] == 2) break;
if (need[i][j] == 1) depth[i] = j;
if (need[i][j] == 0) depth[i] = max(depth[i], min((i > 0 ? depth[i - 1] : (int)(1e9)), j));
}
//cout << depth[i] << " ";
}
//cout << "\n";
for (int i = 0; i < n - 1; i++) {
if (depth[i] < depth[i + 1]) 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 = 1e11;
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";
return l;
}
vector<vector<int>> fliph(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>> 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[n - 1 - i][j];
}
}
return ans;
}
vector<vector<int>> flips(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][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];
int ans = solve(n, m, a);
a = fliph(n, m, a);
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
a = fliph(n, m, a);
ans = min(ans, solve(n, m, a));
a = flips(n, m, a);
ans = min(ans, solve(n, m, a));
a = fliph(n, m, a);
ans = min(ans, solve(n, m, a));
a = flipv(n, m, a);
ans = min(ans, solve(n, m, a));
a = fliph(n, m, a);
ans = min(ans, solve(n, m, a));
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 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 |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
58 ms |
1128 KB |
Output is correct |
17 |
Correct |
59 ms |
1368 KB |
Output is correct |
18 |
Correct |
62 ms |
1356 KB |
Output is correct |
19 |
Correct |
62 ms |
1684 KB |
Output is correct |
20 |
Correct |
82 ms |
1268 KB |
Output is correct |
21 |
Correct |
59 ms |
1492 KB |
Output is correct |
22 |
Correct |
57 ms |
1476 KB |
Output is correct |
23 |
Correct |
64 ms |
1488 KB |
Output is correct |
24 |
Correct |
55 ms |
1360 KB |
Output is correct |
25 |
Correct |
101 ms |
1484 KB |
Output is correct |
26 |
Correct |
61 ms |
1476 KB |
Output is correct |
27 |
Correct |
59 ms |
1472 KB |
Output is correct |
28 |
Correct |
59 ms |
1476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 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 |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
1 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 |
4 ms |
348 KB |
Output is correct |
16 |
Correct |
58 ms |
1128 KB |
Output is correct |
17 |
Correct |
59 ms |
1368 KB |
Output is correct |
18 |
Correct |
62 ms |
1356 KB |
Output is correct |
19 |
Correct |
62 ms |
1684 KB |
Output is correct |
20 |
Correct |
82 ms |
1268 KB |
Output is correct |
21 |
Correct |
59 ms |
1492 KB |
Output is correct |
22 |
Correct |
57 ms |
1476 KB |
Output is correct |
23 |
Correct |
64 ms |
1488 KB |
Output is correct |
24 |
Correct |
55 ms |
1360 KB |
Output is correct |
25 |
Correct |
101 ms |
1484 KB |
Output is correct |
26 |
Correct |
61 ms |
1476 KB |
Output is correct |
27 |
Correct |
59 ms |
1472 KB |
Output is correct |
28 |
Correct |
59 ms |
1476 KB |
Output is correct |
29 |
Execution timed out |
4091 ms |
82228 KB |
Time limit exceeded |
30 |
Halted |
0 ms |
0 KB |
- |