#include "bits/stdc++.h"
using namespace std;
#define forR(i, x) for(int i=0; i<(x); ++i)
#define REP(i, a, b) for(int i=(a); i<(b); ++i)
#define all(x) x.begin(), x.end()
#define boost() cin.sync_with_stdio(0); cin.tie(0)
typedef long long ll;
typedef vector<int> vi;
const int MN = 2010;
int a[MN][MN];
int h, w;
struct cell{int r, c, val;};
cell bv[MN * MN];
int osa[MN][MN], xsa[MN][MN];
void setOnes(int lBound, int hBound) {
REP(r, 1, h+1) REP(c, 1, w+1) osa[r][c] = xsa[r][c] = 0;
forR(i, h*w) {
if(bv[i].val <= lBound) osa[bv[i].r][bv[i].c] += 1;
if(bv[i].val >= hBound) xsa[bv[i].r][bv[i].c] += 1;
}
}
void propagate(int arr[MN][MN], int dr, int dc) {
for(int i = (dr == 1 ? 1 : h); i > 0 && i <= h; i += dr) {
for(int j = (dc == 1 ? 1 : w); j > 0 && j <= w; j += dc) {
arr[i][j] += arr[i-dr][j] + arr[i][j-dc] - arr[i-dr][j-dc];
}
}
}
bool hasCommon() {
REP(r, 1, h+1) REP(c, 1, w+1) if(osa[r][c] > 0 && xsa[r][c] > 0) return true;
return false;
}
bool possible(int mDiff) {
int lBound = bv[h*w-1].val - mDiff - 1;
int hBound = bv[0].val + mDiff + 1;
setOnes(lBound, hBound);
propagate(osa, 1, 1);
propagate(xsa, -1, -1);
if(!hasCommon()) return true;
setOnes(lBound, hBound);
propagate(osa, 1, -1);
propagate(xsa, -1, 1);
if(!hasCommon()) return true;
setOnes(lBound, hBound);
propagate(osa, -1, 1);
propagate(xsa, 1, -1);
if(!hasCommon()) return true;
setOnes(lBound, hBound);
propagate(osa, -1, -1);
propagate(xsa, 1, 1);
if(!hasCommon()) return true;
return false;
}
int calcAns() {
int cells = h * w;
sort(bv, bv + cells, [](cell a, cell b){return a.val < b.val;});
possible(5);
int lo = -1, hi = 1e9;
while(hi - lo > 1) {
int mid = (lo+hi)/2;
if(possible(mid)) hi = mid;
else lo = mid;
}
return hi;
}
signed main() {
boost();
cin >> h >> w;
REP(r, 1, h+1) REP(c, 1, w+1) {
cin >> a[r][c];
bv[(r-1)*w + (c-1)] = {r, c, a[r][c]};
}
int bes = calcAns();
cout << bes << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
12 ms |
12964 KB |
Output is correct |
17 |
Correct |
20 ms |
13152 KB |
Output is correct |
18 |
Correct |
26 ms |
13148 KB |
Output is correct |
19 |
Correct |
17 ms |
13148 KB |
Output is correct |
20 |
Correct |
16 ms |
12892 KB |
Output is correct |
21 |
Correct |
25 ms |
13276 KB |
Output is correct |
22 |
Correct |
24 ms |
13148 KB |
Output is correct |
23 |
Correct |
25 ms |
13156 KB |
Output is correct |
24 |
Correct |
30 ms |
13144 KB |
Output is correct |
25 |
Correct |
28 ms |
13148 KB |
Output is correct |
26 |
Correct |
22 ms |
13148 KB |
Output is correct |
27 |
Correct |
26 ms |
13144 KB |
Output is correct |
28 |
Correct |
27 ms |
13272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
6488 KB |
Output is correct |
2 |
Correct |
1 ms |
6492 KB |
Output is correct |
3 |
Correct |
1 ms |
6492 KB |
Output is correct |
4 |
Correct |
1 ms |
6492 KB |
Output is correct |
5 |
Correct |
1 ms |
6492 KB |
Output is correct |
6 |
Correct |
1 ms |
6492 KB |
Output is correct |
7 |
Correct |
1 ms |
6492 KB |
Output is correct |
8 |
Correct |
1 ms |
6488 KB |
Output is correct |
9 |
Correct |
1 ms |
6492 KB |
Output is correct |
10 |
Correct |
1 ms |
6492 KB |
Output is correct |
11 |
Correct |
1 ms |
6488 KB |
Output is correct |
12 |
Correct |
2 ms |
6492 KB |
Output is correct |
13 |
Correct |
1 ms |
6492 KB |
Output is correct |
14 |
Correct |
1 ms |
6492 KB |
Output is correct |
15 |
Correct |
2 ms |
6488 KB |
Output is correct |
16 |
Correct |
12 ms |
12964 KB |
Output is correct |
17 |
Correct |
20 ms |
13152 KB |
Output is correct |
18 |
Correct |
26 ms |
13148 KB |
Output is correct |
19 |
Correct |
17 ms |
13148 KB |
Output is correct |
20 |
Correct |
16 ms |
12892 KB |
Output is correct |
21 |
Correct |
25 ms |
13276 KB |
Output is correct |
22 |
Correct |
24 ms |
13148 KB |
Output is correct |
23 |
Correct |
25 ms |
13156 KB |
Output is correct |
24 |
Correct |
30 ms |
13144 KB |
Output is correct |
25 |
Correct |
28 ms |
13148 KB |
Output is correct |
26 |
Correct |
22 ms |
13148 KB |
Output is correct |
27 |
Correct |
26 ms |
13144 KB |
Output is correct |
28 |
Correct |
27 ms |
13272 KB |
Output is correct |
29 |
Correct |
2475 ms |
113492 KB |
Output is correct |
30 |
Correct |
2059 ms |
113556 KB |
Output is correct |
31 |
Correct |
2237 ms |
118096 KB |
Output is correct |
32 |
Correct |
1989 ms |
118100 KB |
Output is correct |
33 |
Correct |
2600 ms |
104488 KB |
Output is correct |
34 |
Correct |
2230 ms |
118384 KB |
Output is correct |
35 |
Correct |
2930 ms |
133940 KB |
Output is correct |
36 |
Correct |
3363 ms |
123140 KB |
Output is correct |
37 |
Correct |
2895 ms |
134116 KB |
Output is correct |