//#pragma GCC optimize("O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")
//#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2000;
int a[N][N];
int bot[N];
int b[N];
int mx;
int solve(int h, int w) {
for (int i = 0; i < h; i++) {
b[i] = -1;
for (int j = 0; j < w; j++) {
if (a[i][j] == mx) {
b[i] = j;
}
}
}
int l = -1, r = 1e9;
while (l + 1 < r) {
int mid = (l + r) / 2;
for (int i = 0; i < h; i++) {
bot[i] = w;
for (int j = 0; j < w; j++) {
if (a[i][j] + mid < mx) {
bot[i] = j;
break;
}
}
}
//decreasing
int d = w - 1;
bool f = true;
int kek1 = mx;
int kek2 = 0;
for (int i = 0; i < h; i++) {
d = min(d, bot[i] - 1);
if (d < b[i]) {
f = false;
break;
}
for (int j = d + 1; j < w; j++) {
kek1 = min(kek1, a[i][j]);
kek2 = max(kek2, a[i][j]);
}
}
if (f && kek2 - kek1 <= mid) {
r = mid;
continue;
}
l = mid;
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int h, w;
cin >> h >> w;
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) {
cin >> a[i][j];
mx = max(mx, a[i][j]);
}
}
int ans = solve(h, w);
for (int i = 0; i < h; i++) {
for (int j = 0; j < w / 2; j++) {
swap(a[i][j], a[i][w - j - 1]);
}
}
ans = min(ans, solve(h, w));
for (int j = 0; j < w; j++) {
for (int i = 0; i < h / 2; i++) {
swap(a[i][j], a[h - i - 1][j]);
}
}
ans = min(ans, solve(h, w));
for (int i = 0; i < h; i++) {
for (int j = 0; j < w / 2; j++) {
swap(a[i][j], a[i][w - j - 1]);
}
}
ans = min(ans, solve(h, w));
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1272 KB |
Output is correct |
17 |
Correct |
13 ms |
1272 KB |
Output is correct |
18 |
Correct |
12 ms |
1272 KB |
Output is correct |
19 |
Correct |
13 ms |
1272 KB |
Output is correct |
20 |
Correct |
11 ms |
1400 KB |
Output is correct |
21 |
Correct |
14 ms |
1656 KB |
Output is correct |
22 |
Correct |
14 ms |
1656 KB |
Output is correct |
23 |
Correct |
14 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
14 ms |
1704 KB |
Output is correct |
26 |
Correct |
15 ms |
1652 KB |
Output is correct |
27 |
Correct |
14 ms |
1656 KB |
Output is correct |
28 |
Correct |
14 ms |
1656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 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 |
380 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
376 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
376 KB |
Output is correct |
13 |
Correct |
2 ms |
380 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
9 ms |
1272 KB |
Output is correct |
17 |
Correct |
13 ms |
1272 KB |
Output is correct |
18 |
Correct |
12 ms |
1272 KB |
Output is correct |
19 |
Correct |
13 ms |
1272 KB |
Output is correct |
20 |
Correct |
11 ms |
1400 KB |
Output is correct |
21 |
Correct |
14 ms |
1656 KB |
Output is correct |
22 |
Correct |
14 ms |
1656 KB |
Output is correct |
23 |
Correct |
14 ms |
1656 KB |
Output is correct |
24 |
Correct |
13 ms |
1528 KB |
Output is correct |
25 |
Correct |
14 ms |
1704 KB |
Output is correct |
26 |
Correct |
15 ms |
1652 KB |
Output is correct |
27 |
Correct |
14 ms |
1656 KB |
Output is correct |
28 |
Correct |
14 ms |
1656 KB |
Output is correct |
29 |
Correct |
842 ms |
37324 KB |
Output is correct |
30 |
Correct |
785 ms |
37624 KB |
Output is correct |
31 |
Correct |
846 ms |
39160 KB |
Output is correct |
32 |
Correct |
846 ms |
39160 KB |
Output is correct |
33 |
Correct |
715 ms |
34296 KB |
Output is correct |
34 |
Correct |
858 ms |
39292 KB |
Output is correct |
35 |
Correct |
1119 ms |
54912 KB |
Output is correct |
36 |
Correct |
1000 ms |
49680 KB |
Output is correct |
37 |
Correct |
1183 ms |
54924 KB |
Output is correct |