#include<bits/stdc++.h>
#define F first
#define S second
#define SZ(x) ((int)(x).size())
inline void outlast() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
}
const char nl = '\n';
using namespace std;
using ll = long long;
// head
const int N = 2100;
vector<vector<int>> a(N, vector<int> (N, 0));
vector<vector<int>> rotated(N, vector<int>(N));
int h, w;
vector<vector<int>> CIRCLE(const vector<vector<int>>& matrix) {
for (int i = 1; i <= h; i++) {
for (int j = 1; j <= w; j++) {
rotated[j][h-i+1] = matrix[i][j];
}
}
swap(h, w);
return rotated;
}
void solve() {
cin >> h >> w;
for (int i = 1; i <= h; i ++) {
for (int j = 1; j <= w; j ++) {
cin >> a[i][j];
}
}
int L = 0, R = 1e9;
vector<int> row(N, 0);
while (L + 1 < R) {
int mid = (L + R) / 2;
bool ok = false;
for (int i = 0; i < 4; i ++) {
a = CIRCLE(a);
int val = INT_MAX, posx = 0, posy = 0;
for (int i = 1; i <= h; i ++) {
for (int j = 1; j <= w; j ++) {
if (a[i][j] < val) {
val = a[i][j];
posx = i;
posy = j;
}
}
}
for (int i = 1; i <= h; i ++) {
row[i] = 0;
for (int j = 1; j <= w; j ++) {
if (a[i][j] <= val + mid) {
row[i] ++;
} else {
break;
}
}
}
for (int i = 2; i <= h; i ++) {
row[i] = min(row[i], row[i-1]);
}
int mn = INT_MAX, mx = INT_MIN;
for (int i = 1; i <= h; i ++) {
for (int j = row[i]+1; j <= w; j ++) {
mn = min(mn, a[i][j]);
mx = max(mx, a[i][j]);
}
}
if (row[posx] >= posy && (mx == INT_MIN || mx - mn <= mid)) {
ok = true;
}
}
if (ok) {
R = mid;
} else {
L = mid;
}
}
cout << R;
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// outlast();
int tst = 1;
// cin >> tst;
while (tst --) {
solve();
cout << nl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
555 ms |
52916 KB |
Output is correct |
2 |
Correct |
528 ms |
52560 KB |
Output is correct |
3 |
Correct |
571 ms |
52616 KB |
Output is correct |
4 |
Correct |
577 ms |
52560 KB |
Output is correct |
5 |
Correct |
546 ms |
52612 KB |
Output is correct |
6 |
Correct |
548 ms |
52540 KB |
Output is correct |
7 |
Correct |
619 ms |
52596 KB |
Output is correct |
8 |
Correct |
579 ms |
52624 KB |
Output is correct |
9 |
Correct |
542 ms |
52576 KB |
Output is correct |
10 |
Correct |
560 ms |
52756 KB |
Output is correct |
11 |
Correct |
585 ms |
52564 KB |
Output is correct |
12 |
Correct |
554 ms |
52760 KB |
Output is correct |
13 |
Correct |
550 ms |
52580 KB |
Output is correct |
14 |
Correct |
550 ms |
52656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
555 ms |
52916 KB |
Output is correct |
2 |
Correct |
528 ms |
52560 KB |
Output is correct |
3 |
Correct |
571 ms |
52616 KB |
Output is correct |
4 |
Correct |
577 ms |
52560 KB |
Output is correct |
5 |
Correct |
546 ms |
52612 KB |
Output is correct |
6 |
Correct |
548 ms |
52540 KB |
Output is correct |
7 |
Correct |
619 ms |
52596 KB |
Output is correct |
8 |
Correct |
579 ms |
52624 KB |
Output is correct |
9 |
Correct |
542 ms |
52576 KB |
Output is correct |
10 |
Correct |
560 ms |
52756 KB |
Output is correct |
11 |
Correct |
585 ms |
52564 KB |
Output is correct |
12 |
Correct |
554 ms |
52760 KB |
Output is correct |
13 |
Correct |
550 ms |
52580 KB |
Output is correct |
14 |
Correct |
550 ms |
52656 KB |
Output is correct |
15 |
Correct |
571 ms |
52604 KB |
Output is correct |
16 |
Correct |
578 ms |
52576 KB |
Output is correct |
17 |
Correct |
580 ms |
52612 KB |
Output is correct |
18 |
Correct |
574 ms |
52688 KB |
Output is correct |
19 |
Correct |
570 ms |
52588 KB |
Output is correct |
20 |
Correct |
579 ms |
52612 KB |
Output is correct |
21 |
Correct |
613 ms |
52668 KB |
Output is correct |
22 |
Correct |
585 ms |
52560 KB |
Output is correct |
23 |
Correct |
583 ms |
52780 KB |
Output is correct |
24 |
Correct |
587 ms |
52580 KB |
Output is correct |
25 |
Correct |
591 ms |
52576 KB |
Output is correct |
26 |
Correct |
601 ms |
52576 KB |
Output is correct |
27 |
Correct |
582 ms |
52560 KB |
Output is correct |
28 |
Correct |
624 ms |
52544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
555 ms |
52916 KB |
Output is correct |
2 |
Correct |
528 ms |
52560 KB |
Output is correct |
3 |
Correct |
571 ms |
52616 KB |
Output is correct |
4 |
Correct |
577 ms |
52560 KB |
Output is correct |
5 |
Correct |
546 ms |
52612 KB |
Output is correct |
6 |
Correct |
548 ms |
52540 KB |
Output is correct |
7 |
Correct |
619 ms |
52596 KB |
Output is correct |
8 |
Correct |
579 ms |
52624 KB |
Output is correct |
9 |
Correct |
542 ms |
52576 KB |
Output is correct |
10 |
Correct |
560 ms |
52756 KB |
Output is correct |
11 |
Correct |
585 ms |
52564 KB |
Output is correct |
12 |
Correct |
554 ms |
52760 KB |
Output is correct |
13 |
Correct |
550 ms |
52580 KB |
Output is correct |
14 |
Correct |
550 ms |
52656 KB |
Output is correct |
15 |
Correct |
571 ms |
52604 KB |
Output is correct |
16 |
Correct |
578 ms |
52576 KB |
Output is correct |
17 |
Correct |
580 ms |
52612 KB |
Output is correct |
18 |
Correct |
574 ms |
52688 KB |
Output is correct |
19 |
Correct |
570 ms |
52588 KB |
Output is correct |
20 |
Correct |
579 ms |
52612 KB |
Output is correct |
21 |
Correct |
613 ms |
52668 KB |
Output is correct |
22 |
Correct |
585 ms |
52560 KB |
Output is correct |
23 |
Correct |
583 ms |
52780 KB |
Output is correct |
24 |
Correct |
587 ms |
52580 KB |
Output is correct |
25 |
Correct |
591 ms |
52576 KB |
Output is correct |
26 |
Correct |
601 ms |
52576 KB |
Output is correct |
27 |
Correct |
582 ms |
52560 KB |
Output is correct |
28 |
Correct |
624 ms |
52544 KB |
Output is correct |
29 |
Correct |
2987 ms |
52444 KB |
Output is correct |
30 |
Correct |
2897 ms |
52632 KB |
Output is correct |
31 |
Correct |
3297 ms |
52480 KB |
Output is correct |
32 |
Correct |
3183 ms |
75712 KB |
Output is correct |
33 |
Correct |
2758 ms |
72500 KB |
Output is correct |
34 |
Correct |
3080 ms |
75636 KB |
Output is correct |
35 |
Correct |
3243 ms |
91156 KB |
Output is correct |
36 |
Correct |
2685 ms |
86144 KB |
Output is correct |
37 |
Correct |
3029 ms |
91268 KB |
Output is correct |