#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;
typedef vector<pair<int, int> > vii;
typedef vector<vector<pair<int, int> > > vvii;
#define pb(x) push_back(x)
#define mp(x, y) make_pair(x, y)
int H, W;
vector<pair<int, ii> > arr;
int ans = (int)1e9+7;
int fw1[2049], fw2[2049];
int fw1get(int pos) {
pos += 1;
int mxmm = -1;
while (pos <= 2048) {
mxmm = max(mxmm, fw1[pos]);
pos += pos & -pos;
}
return mxmm;
}
int fw2get(int pos) {
pos += 1;
int mxmm = -1;
while (pos <= 2048) {
mxmm = max(mxmm, fw2[pos]);
pos += pos & -pos;
}
return mxmm;
}
void fw1max(int pos, int val) {
pos += 1;
while (pos > 0) {
fw1[pos] = max(fw1[pos], val);
pos -= pos & -pos;
}
}
void fw2max(int pos, int val) {
pos += 1;
while (pos > 0) {
fw2[pos] = max(fw2[pos], val);
pos -= pos & -pos;
}
}
void calc() {
for (int i = 0; i < 2049; i++) {
fw1[i] = -1;
fw2[i] = -1;
}
int i = 0, j = arr.size()-1;
if (arr[j].second.first <= arr[i].second.first && arr[j].second.second <= arr[i].second.second) return;
fw1max(arr[i].second.second, arr[i].second.first);
fw2max(W-arr[j].second.second-1, H - arr[j].second.first - 1);
int mini = arr[i].first, maxi = arr[j].first;
int worst1 = 0, worst2 = 0;
while (true) {
worst1 = arr[j-1].first - mini;
worst2 = maxi - arr[i+1].first;
if (j == i+1) break;
if (worst1 < worst2) {
i++;
ii pos = arr[i].second;
int wall = fw2get(W-pos.second-1);
if (pos.first >= H - wall - 1) break;
fw1max(pos.second, pos.first);
}
else {
j--;
ii pos = arr[j].second;
int wall = fw1get(pos.second);
if (pos.first <= wall) break;
fw2max(W-pos.second-1, H - pos.first - 1);
}
}
//cerr << i << "," << j << " " << worst1 << " " << worst2 << endl;
ans = min(ans, max(worst1, worst2));
}
void rotate() {
for (int i = 0; i < arr.size(); i++) {
arr[i].second = mp(arr[i].second.second, H-arr[i].second.first-1);
}
H = H ^ W;
W = H ^ W;
H = H ^ W;
}
void dump() {
int mat[H][W];
for (int i = 0; i < arr.size(); i++) {
mat[arr[i].second.first][arr[i].second.second] = arr[i].first;
}
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
cerr << mat[i][j] << " ";
} cerr << endl;
}
}
int main(int argc, char **argv) {
//freopen("input", "r", stdin);
//freopen("output", "w", stdout);
scanf("%d%d", &H, &W);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
int x; scanf("%d", &x);
arr.pb(mp(x, mp(i, j)));
}
}
sort(arr.begin(), arr.end());
calc();
rotate();
//dump();
calc();
rotate();
//dump();
calc();
rotate();
//dump();
calc();
printf("%d\n", ans);
return 0;
}
Compilation message
joioi.cpp: In function 'void rotate()':
joioi.cpp:85:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); i++) {
^
joioi.cpp: In function 'void dump()':
joioi.cpp:95:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < arr.size(); i++) {
^
joioi.cpp: In function 'int main(int, char**)':
joioi.cpp:110:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &H, &W);
^
joioi.cpp:113:26: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int x; scanf("%d", &x);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2040 KB |
Output is correct |
2 |
Correct |
0 ms |
2040 KB |
Output is correct |
3 |
Correct |
0 ms |
2040 KB |
Output is correct |
4 |
Correct |
0 ms |
2040 KB |
Output is correct |
5 |
Correct |
0 ms |
2040 KB |
Output is correct |
6 |
Correct |
0 ms |
2040 KB |
Output is correct |
7 |
Correct |
0 ms |
2040 KB |
Output is correct |
8 |
Correct |
0 ms |
2040 KB |
Output is correct |
9 |
Correct |
0 ms |
2040 KB |
Output is correct |
10 |
Correct |
0 ms |
2040 KB |
Output is correct |
11 |
Correct |
0 ms |
2040 KB |
Output is correct |
12 |
Correct |
0 ms |
2040 KB |
Output is correct |
13 |
Correct |
0 ms |
2040 KB |
Output is correct |
14 |
Correct |
0 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2040 KB |
Output is correct |
2 |
Correct |
0 ms |
2040 KB |
Output is correct |
3 |
Correct |
0 ms |
2040 KB |
Output is correct |
4 |
Correct |
0 ms |
2040 KB |
Output is correct |
5 |
Correct |
0 ms |
2040 KB |
Output is correct |
6 |
Correct |
0 ms |
2040 KB |
Output is correct |
7 |
Correct |
0 ms |
2040 KB |
Output is correct |
8 |
Correct |
0 ms |
2040 KB |
Output is correct |
9 |
Correct |
0 ms |
2040 KB |
Output is correct |
10 |
Correct |
0 ms |
2040 KB |
Output is correct |
11 |
Correct |
0 ms |
2040 KB |
Output is correct |
12 |
Correct |
0 ms |
2040 KB |
Output is correct |
13 |
Correct |
0 ms |
2040 KB |
Output is correct |
14 |
Correct |
0 ms |
2040 KB |
Output is correct |
15 |
Correct |
0 ms |
2212 KB |
Output is correct |
16 |
Correct |
9 ms |
3276 KB |
Output is correct |
17 |
Correct |
9 ms |
3276 KB |
Output is correct |
18 |
Correct |
13 ms |
3276 KB |
Output is correct |
19 |
Correct |
13 ms |
3276 KB |
Output is correct |
20 |
Correct |
0 ms |
3276 KB |
Output is correct |
21 |
Correct |
9 ms |
3276 KB |
Output is correct |
22 |
Correct |
9 ms |
3276 KB |
Output is correct |
23 |
Correct |
13 ms |
3276 KB |
Output is correct |
24 |
Correct |
9 ms |
3276 KB |
Output is correct |
25 |
Correct |
9 ms |
3276 KB |
Output is correct |
26 |
Correct |
13 ms |
3276 KB |
Output is correct |
27 |
Correct |
9 ms |
3276 KB |
Output is correct |
28 |
Correct |
6 ms |
3276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
2040 KB |
Output is correct |
2 |
Correct |
0 ms |
2040 KB |
Output is correct |
3 |
Correct |
0 ms |
2040 KB |
Output is correct |
4 |
Correct |
0 ms |
2040 KB |
Output is correct |
5 |
Correct |
0 ms |
2040 KB |
Output is correct |
6 |
Correct |
0 ms |
2040 KB |
Output is correct |
7 |
Correct |
0 ms |
2040 KB |
Output is correct |
8 |
Correct |
0 ms |
2040 KB |
Output is correct |
9 |
Correct |
0 ms |
2040 KB |
Output is correct |
10 |
Correct |
0 ms |
2040 KB |
Output is correct |
11 |
Correct |
0 ms |
2040 KB |
Output is correct |
12 |
Correct |
0 ms |
2040 KB |
Output is correct |
13 |
Correct |
0 ms |
2040 KB |
Output is correct |
14 |
Correct |
0 ms |
2040 KB |
Output is correct |
15 |
Correct |
0 ms |
2212 KB |
Output is correct |
16 |
Correct |
9 ms |
3276 KB |
Output is correct |
17 |
Correct |
9 ms |
3276 KB |
Output is correct |
18 |
Correct |
13 ms |
3276 KB |
Output is correct |
19 |
Correct |
13 ms |
3276 KB |
Output is correct |
20 |
Correct |
0 ms |
3276 KB |
Output is correct |
21 |
Correct |
9 ms |
3276 KB |
Output is correct |
22 |
Correct |
9 ms |
3276 KB |
Output is correct |
23 |
Correct |
13 ms |
3276 KB |
Output is correct |
24 |
Correct |
9 ms |
3276 KB |
Output is correct |
25 |
Correct |
9 ms |
3276 KB |
Output is correct |
26 |
Correct |
13 ms |
3276 KB |
Output is correct |
27 |
Correct |
9 ms |
3276 KB |
Output is correct |
28 |
Correct |
6 ms |
3276 KB |
Output is correct |
29 |
Correct |
1193 ms |
75852 KB |
Output is correct |
30 |
Correct |
1186 ms |
75852 KB |
Output is correct |
31 |
Correct |
1319 ms |
75852 KB |
Output is correct |
32 |
Correct |
1239 ms |
75852 KB |
Output is correct |
33 |
Correct |
1243 ms |
75852 KB |
Output is correct |
34 |
Correct |
1309 ms |
75852 KB |
Output is correct |
35 |
Correct |
1293 ms |
75852 KB |
Output is correct |
36 |
Correct |
1256 ms |
75852 KB |
Output is correct |
37 |
Correct |
1273 ms |
75852 KB |
Output is correct |