#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
#define ll long long
#define ull unsigned long long
// 217
// 44
using namespace std;
const int INF = 1e9;
const int MAXN = 2000;
int mat[MAXN + 1][MAXN + 1], aux[MAXN + 1][MAXN + 1];
int n, m, mn, mx;
inline void rot() {
int i, j;
for(i = 1; i <= m; i++) {
for(j = 1; j <= n; j++) {
aux[i][j] = mat[n - j + 1][i];
}
}
swap(n, m);
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
mat[i][j] = aux[i][j];
}
}
}
inline bool solve(int dif) {
int l = INF, r = -INF;
int last = m;
/*for(int j = 1; j <= m; j++) {
int i = 1;
while(i <= min(n, last) && mat[i][j] <= mn + dif) {
i++;
}
last = min(last, i - 1);
while(i <= n) {
l = min(l, mat[i][j]);
r = max(r, mat[i][j]);
i++;
}
}
if(r - l <= dif) {
return 1;
}*/
last = 1;
l = INF, r = -INF;
for(int j = 1; j <= m; j++) {
int i = n;
while(i >= max(1, last) && mat[i][j] <= mn + dif) {
i--;
}
last = max(last, i + 1);
while(i >= 1) {
l = min(l, mat[i][j]);
r = max(r, mat[i][j]);
if(r - l > dif) {
return 0;
}
i--;
}
}
return r - l <= dif;
}
inline bool check(int dif) {
bool ans = 0;
for(int i = 0; i < 4 && ans == 0; i++) {
ans |= solve(dif);
rot();
}
return ans;
}
int main() {
//ifstream cin("A.in");
//ofstream cout("A.out");
int i, j;
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m;
mx = -INF, mn = INF;
for(i = 1; i <= n; i++) {
for(j = 1; j <= m; j++) {
cin >> mat[i][j];
mn = min(mn, mat[i][j]);
mx = max(mx, mat[i][j]);
}
}
int res = -1;
for(int step = 1 << 29; step; step >>= 1) {
if(res + step <= mx - mn && check(res + step) == 0) {
res += step;
}
}
cout << res + 1;
//cin.close();
//cout.close();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
1920 KB |
Output is correct |
16 |
Correct |
5 ms |
2304 KB |
Output is correct |
17 |
Correct |
10 ms |
2304 KB |
Output is correct |
18 |
Correct |
12 ms |
2304 KB |
Output is correct |
19 |
Correct |
12 ms |
2304 KB |
Output is correct |
20 |
Correct |
10 ms |
2304 KB |
Output is correct |
21 |
Correct |
15 ms |
2304 KB |
Output is correct |
22 |
Correct |
16 ms |
2304 KB |
Output is correct |
23 |
Correct |
17 ms |
2304 KB |
Output is correct |
24 |
Correct |
15 ms |
2304 KB |
Output is correct |
25 |
Correct |
16 ms |
2304 KB |
Output is correct |
26 |
Correct |
14 ms |
2304 KB |
Output is correct |
27 |
Correct |
15 ms |
2304 KB |
Output is correct |
28 |
Correct |
15 ms |
2304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
512 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
2 ms |
384 KB |
Output is correct |
12 |
Correct |
2 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
3 ms |
1920 KB |
Output is correct |
16 |
Correct |
5 ms |
2304 KB |
Output is correct |
17 |
Correct |
10 ms |
2304 KB |
Output is correct |
18 |
Correct |
12 ms |
2304 KB |
Output is correct |
19 |
Correct |
12 ms |
2304 KB |
Output is correct |
20 |
Correct |
10 ms |
2304 KB |
Output is correct |
21 |
Correct |
15 ms |
2304 KB |
Output is correct |
22 |
Correct |
16 ms |
2304 KB |
Output is correct |
23 |
Correct |
17 ms |
2304 KB |
Output is correct |
24 |
Correct |
15 ms |
2304 KB |
Output is correct |
25 |
Correct |
16 ms |
2304 KB |
Output is correct |
26 |
Correct |
14 ms |
2304 KB |
Output is correct |
27 |
Correct |
15 ms |
2304 KB |
Output is correct |
28 |
Correct |
15 ms |
2304 KB |
Output is correct |
29 |
Correct |
2117 ms |
31840 KB |
Output is correct |
30 |
Correct |
2037 ms |
31980 KB |
Output is correct |
31 |
Correct |
2105 ms |
35320 KB |
Output is correct |
32 |
Correct |
1670 ms |
34988 KB |
Output is correct |
33 |
Correct |
1936 ms |
34636 KB |
Output is correct |
34 |
Correct |
2158 ms |
35012 KB |
Output is correct |
35 |
Correct |
3532 ms |
37752 KB |
Output is correct |
36 |
Correct |
3517 ms |
35948 KB |
Output is correct |
37 |
Execution timed out |
4013 ms |
37880 KB |
Time limit exceeded |