#include <bits/stdc++.h>
#define ll long long
#define ull unsigned long long
#define dl double
#define st first
#define nd second
#define II pair <int, int>
using namespace std;
const int N = 5 + 1e5;
const int inf = 7 + 1e9;
int m, n, ma = 0;
vector <vector <int>> a;
int check(int lim) {
vector <int> len(m + 1, n);
for (int i = 1; i <= m; i ++)
for (int j = n; j > 0; j --) {
if (a[i][j] < ma - lim)
break;
len[i] --;
}
int val = 0, x = inf, y = 0, check = 0;
for (int i = 1; i <= m; i ++) {
val = max(val, len[i]);
if (val < n)
check = 1;
for (int j = 1; j <= val; j ++) {
x = min(x, a[i][j]);
y = max(y, a[i][j]);
}
}
if (check && y - x <= lim)
return 1;
val = 0, x = inf, y = 0, check = 0;
for (int i = m; i > 0; i --) {
val = max(val, len[i]);
if (val < n)
check = 1;
for (int j = 1; j <= val; j ++) {
x = min(x, a[i][j]);
y = max(y, a[i][j]);
}
}
return check && y - x <= lim;
}
int cal() {
vector <vector <int>> b = a;
for (int i = 1; i <= m; i ++)
for (int j = 1; j <= n; j ++)
b[j][m - i + 1] = a[i][j];
a = b;
swap(m, n);
int lo = 0, hi = inf;
while (lo < hi) {
int mid = (lo + hi) / 2;
if (check(mid))
hi = mid;
else
lo = mid + 1;
}
return lo;
}
int main() {
#define TASKNAME ""
ios_base :: sync_with_stdio (0);
cin.tie (0);
if ( fopen( TASKNAME".inp", "r" ) ) {
freopen (TASKNAME".inp", "r", stdin);
freopen (TASKNAME".out", "w", stdout);
}
cin >> m >> n;
a.assign(max(m, n) + 1, vector <int> (max(m, n) + 1, 0));
for (int i = 1; i <= m; i ++)
for (int j = 1; j <= n; j ++) {
cin >> a[i][j];
ma = max(ma, a[i][j]);
}
int ans = inf;
for (int i = 0; i < 4; i ++)
ans = min(ans, cal());
cout << ans;
return 0;
}
Compilation message
joioi.cpp: In function 'int main()':
joioi.cpp:73:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen (TASKNAME".inp", "r", stdin);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
joioi.cpp:74:17: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | freopen (TASKNAME".out", "w", stdout);
| ~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
500 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 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 |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
500 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 |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
720 KB |
Output is correct |
17 |
Correct |
8 ms |
860 KB |
Output is correct |
18 |
Correct |
9 ms |
860 KB |
Output is correct |
19 |
Correct |
8 ms |
1060 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
13 ms |
1176 KB |
Output is correct |
22 |
Correct |
12 ms |
1116 KB |
Output is correct |
23 |
Correct |
12 ms |
992 KB |
Output is correct |
24 |
Correct |
11 ms |
1116 KB |
Output is correct |
25 |
Correct |
12 ms |
1116 KB |
Output is correct |
26 |
Correct |
13 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1116 KB |
Output is correct |
28 |
Correct |
12 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
452 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 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 |
500 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 |
604 KB |
Output is correct |
16 |
Correct |
5 ms |
720 KB |
Output is correct |
17 |
Correct |
8 ms |
860 KB |
Output is correct |
18 |
Correct |
9 ms |
860 KB |
Output is correct |
19 |
Correct |
8 ms |
1060 KB |
Output is correct |
20 |
Correct |
8 ms |
860 KB |
Output is correct |
21 |
Correct |
13 ms |
1176 KB |
Output is correct |
22 |
Correct |
12 ms |
1116 KB |
Output is correct |
23 |
Correct |
12 ms |
992 KB |
Output is correct |
24 |
Correct |
11 ms |
1116 KB |
Output is correct |
25 |
Correct |
12 ms |
1116 KB |
Output is correct |
26 |
Correct |
13 ms |
1116 KB |
Output is correct |
27 |
Correct |
11 ms |
1116 KB |
Output is correct |
28 |
Correct |
12 ms |
1116 KB |
Output is correct |
29 |
Correct |
1130 ms |
53968 KB |
Output is correct |
30 |
Correct |
1090 ms |
53392 KB |
Output is correct |
31 |
Correct |
991 ms |
55016 KB |
Output is correct |
32 |
Correct |
1014 ms |
55124 KB |
Output is correct |
33 |
Correct |
1057 ms |
51524 KB |
Output is correct |
34 |
Correct |
1289 ms |
55200 KB |
Output is correct |
35 |
Correct |
1378 ms |
70604 KB |
Output is correct |
36 |
Correct |
1151 ms |
65264 KB |
Output is correct |
37 |
Correct |
1403 ms |
70780 KB |
Output is correct |