#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = a; i<=b ; i++)
#define FORD(i, a, b) for (int i = a; i>=b; i--)
#define REP(i, a) for (int i = 0; i<a; i++)
#define N 2001
#define pp pair<int, int>
#define IO cin.tie(NULL);cout.tie(NULL);
template<typename T> inline void read(T &x) {
char c;
bool neg = false;
while (!isdigit(c = getchar()) && c != '-');
x = 0;
if (c == '-') {
neg = true;
c = getchar();
}
do {
x = x * 10 + c - '0';
} while (isdigit(c = getchar()));
if (neg) x = -x;
}
template<typename T> inline void write(T x) {
if (x < 0) {
putchar('-');
write(-x);
return;
}
if (x < 10) {
putchar(char(x + 48));
}
else {
write(x/10);
putchar(char(48 + x%10));
}
}
template<typename T> inline void writeln(T x) {
write(x);
putchar('\n');
}
using namespace std;
int n, m, a[N][N], b[N][N];
bool check(int x, int minAll) {
int minA = 1e9 + 1;
int maxA = 0;
int flag = n;
FOR(i, 1, m) {
FOR(j, 1, flag) if (a[i][j] > x) {
FOR(k, j, n) {
minA = min(minA, a[i][k]);
maxA = max(maxA, a[i][k]);
}
flag = j - 1;
break;
}
FOR(j, flag + 1, n) {
minA = min(minA, a[i][j]);
maxA = max(maxA, a[i][j]);
}
}
return maxA - minA <= x - minAll;
}
void Rotate() {
FOR(i, 1, n) FOR(j, 1, m) b[i][j] = a[j][n - i + 1];
swap(n, m);
FOR(i, 1, m) FOR(j, 1, n) a[i][j] = b[i][j];
}
bool ok(int x, int minAll) {
if (check(x, minAll)) return true;
Rotate();
if (check(x, minAll)) return true;
Rotate();
if (check(x, minAll)) return true;
Rotate();
if (check(x, minAll)) return true;
return false;
}
int main() {
IO;
read(m);read(n);
int minA = 1e9 + 1;
int maxA = 0;
FOR(i, 1, m) FOR(j, 1, n) {
read(a[i][j]);
minA = min(minA, a[i][j]);
maxA = max(maxA, a[i][j]);
}
int l = 0;
int r = maxA - minA;
int cur = -1;
while (l <= r) {
int mid = (l + r) >> 1;
if (ok(mid + minA, minA)) {
cur = mid;
r = mid - 1;
} else l = mid + 1;
}
writeln(cur);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
33296 KB |
Output is correct |
2 |
Correct |
0 ms |
33296 KB |
Output is correct |
3 |
Correct |
0 ms |
33296 KB |
Output is correct |
4 |
Correct |
0 ms |
33296 KB |
Output is correct |
5 |
Correct |
0 ms |
33296 KB |
Output is correct |
6 |
Correct |
0 ms |
33296 KB |
Output is correct |
7 |
Correct |
0 ms |
33296 KB |
Output is correct |
8 |
Correct |
0 ms |
33296 KB |
Output is correct |
9 |
Correct |
0 ms |
33296 KB |
Output is correct |
10 |
Correct |
0 ms |
33296 KB |
Output is correct |
11 |
Correct |
0 ms |
33296 KB |
Output is correct |
12 |
Correct |
0 ms |
33296 KB |
Output is correct |
13 |
Correct |
0 ms |
33296 KB |
Output is correct |
14 |
Correct |
0 ms |
33296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
33296 KB |
Output is correct |
2 |
Correct |
0 ms |
33296 KB |
Output is correct |
3 |
Correct |
0 ms |
33296 KB |
Output is correct |
4 |
Correct |
0 ms |
33296 KB |
Output is correct |
5 |
Correct |
0 ms |
33296 KB |
Output is correct |
6 |
Correct |
0 ms |
33296 KB |
Output is correct |
7 |
Correct |
0 ms |
33296 KB |
Output is correct |
8 |
Correct |
0 ms |
33296 KB |
Output is correct |
9 |
Correct |
0 ms |
33296 KB |
Output is correct |
10 |
Correct |
0 ms |
33296 KB |
Output is correct |
11 |
Correct |
0 ms |
33296 KB |
Output is correct |
12 |
Correct |
0 ms |
33296 KB |
Output is correct |
13 |
Correct |
0 ms |
33296 KB |
Output is correct |
14 |
Correct |
0 ms |
33296 KB |
Output is correct |
15 |
Correct |
0 ms |
33296 KB |
Output is correct |
16 |
Correct |
0 ms |
33296 KB |
Output is correct |
17 |
Correct |
9 ms |
33296 KB |
Output is correct |
18 |
Correct |
13 ms |
33296 KB |
Output is correct |
19 |
Correct |
6 ms |
33296 KB |
Output is correct |
20 |
Correct |
9 ms |
33296 KB |
Output is correct |
21 |
Correct |
13 ms |
33296 KB |
Output is correct |
22 |
Correct |
13 ms |
33296 KB |
Output is correct |
23 |
Correct |
13 ms |
33296 KB |
Output is correct |
24 |
Correct |
9 ms |
33296 KB |
Output is correct |
25 |
Correct |
9 ms |
33296 KB |
Output is correct |
26 |
Correct |
9 ms |
33296 KB |
Output is correct |
27 |
Correct |
16 ms |
33296 KB |
Output is correct |
28 |
Correct |
16 ms |
33296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
33296 KB |
Output is correct |
2 |
Correct |
0 ms |
33296 KB |
Output is correct |
3 |
Correct |
0 ms |
33296 KB |
Output is correct |
4 |
Correct |
0 ms |
33296 KB |
Output is correct |
5 |
Correct |
0 ms |
33296 KB |
Output is correct |
6 |
Correct |
0 ms |
33296 KB |
Output is correct |
7 |
Correct |
0 ms |
33296 KB |
Output is correct |
8 |
Correct |
0 ms |
33296 KB |
Output is correct |
9 |
Correct |
0 ms |
33296 KB |
Output is correct |
10 |
Correct |
0 ms |
33296 KB |
Output is correct |
11 |
Correct |
0 ms |
33296 KB |
Output is correct |
12 |
Correct |
0 ms |
33296 KB |
Output is correct |
13 |
Correct |
0 ms |
33296 KB |
Output is correct |
14 |
Correct |
0 ms |
33296 KB |
Output is correct |
15 |
Correct |
0 ms |
33296 KB |
Output is correct |
16 |
Correct |
0 ms |
33296 KB |
Output is correct |
17 |
Correct |
9 ms |
33296 KB |
Output is correct |
18 |
Correct |
13 ms |
33296 KB |
Output is correct |
19 |
Correct |
6 ms |
33296 KB |
Output is correct |
20 |
Correct |
9 ms |
33296 KB |
Output is correct |
21 |
Correct |
13 ms |
33296 KB |
Output is correct |
22 |
Correct |
13 ms |
33296 KB |
Output is correct |
23 |
Correct |
13 ms |
33296 KB |
Output is correct |
24 |
Correct |
9 ms |
33296 KB |
Output is correct |
25 |
Correct |
9 ms |
33296 KB |
Output is correct |
26 |
Correct |
9 ms |
33296 KB |
Output is correct |
27 |
Correct |
16 ms |
33296 KB |
Output is correct |
28 |
Correct |
16 ms |
33296 KB |
Output is correct |
29 |
Correct |
1626 ms |
33296 KB |
Output is correct |
30 |
Correct |
1353 ms |
33296 KB |
Output is correct |
31 |
Correct |
1629 ms |
33296 KB |
Output is correct |
32 |
Correct |
2036 ms |
33296 KB |
Output is correct |
33 |
Correct |
1683 ms |
33296 KB |
Output is correct |
34 |
Correct |
1563 ms |
33296 KB |
Output is correct |
35 |
Correct |
2863 ms |
33296 KB |
Output is correct |
36 |
Correct |
2116 ms |
33296 KB |
Output is correct |
37 |
Correct |
2409 ms |
33296 KB |
Output is correct |