#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int h, w, q;
cin >> h >> w >> q;
vector<int> a(h);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
vector<int> b(w);
for (int i = 0; i < w; i++) {
cin >> b[i];
}
auto Go = [&](int x, int y, int dir) {
if (dir == 0) {
// up
if (x == 0) {
return make_pair(x, y);
}
x--;
while (x > 0 && a[x] < b[y]) {
x -= 1;
}
return make_pair(x, y);
}
if (dir == 1) {
// down
if (x == h - 1) {
return make_pair(x, y);
}
x += 1;
while (x + 1 < h && a[x] < b[y]) {
x += 1;
}
return make_pair(x, y);
}
if (dir == 2) {
// left
if (y == 0) {
return make_pair(x, y);
}
y -= 1;
while (y > 0 && a[x] > b[y]) {
y -= 1;
}
return make_pair(x, y);
}
if (dir == 3) {
// right
if (y == w - 1) {
return make_pair(x, y);
}
y += 1;
while (y + 1 < w && a[x] > b[y]) {
y += 1;
}
return make_pair(x, y);
}
assert(false);
return make_pair(-1, -1);
};
vector<map<pair<int, int>, bool>> was(4);
vector<map<pair<int, int>, int>> dp(4);
function<int(int, int, int)> Solve = [&](int x, int y, int dir) {
if (dir == 0) {
if (a[x] < b[y]) {
return 0;
}
}
if (dir == 1) {
if (a[x] < b[y]) {
return 0;
}
}
if (dir == 2) {
if (a[x] > b[y]) {
return 0;
}
}
if (dir == 3) {
if (a[x] > b[y]) {
return 0;
}
}
pair<int, int> curr = {x, y};
if (was[dir][curr]) {
return dp[dir][curr];
}
was[dir][curr] = true;
if (dir == 0 || dir == 1) {
{
pair<int, int> nxt = Go(x, y, 2);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 2) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
{
pair<int, int> nxt = Go(x, y, 3);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 3) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
}
if (dir == 2 || dir == 3) {
{
pair<int, int> nxt = Go(x, y, 0);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 0) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
{
pair<int, int> nxt = Go(x, y, 1);
if (nxt.first != x || nxt.second != y) {
dp[dir][curr] = max(dp[dir][curr], Solve(nxt.first, nxt.second, 1) + abs(nxt.first - x) + abs(nxt.second - y));
}
}
}
return dp[dir][curr];
};
while (q--) {
int x, y;
cin >> x >> y;
--x; --y;
int res = 1;
for (int dir = 0; dir < 4; dir++) {
pair<int, int> cell = Go(x, y, dir);
int nx = cell.first;
int ny = cell.second;
if (nx != x || ny != y) {
res = max(res, Solve(nx, ny, dir) + abs(x - nx) + abs(y - ny));
}
}
cout << res << '\n';
for (int dir = 0; dir < 4; dir++) {
was[dir].clear();
dp[dir].clear();
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
416 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
728 KB |
Output is correct |
20 |
Correct |
7 ms |
1012 KB |
Output is correct |
21 |
Correct |
6 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
1248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
416 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
728 KB |
Output is correct |
20 |
Correct |
7 ms |
1012 KB |
Output is correct |
21 |
Correct |
6 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
1248 KB |
Output is correct |
23 |
Correct |
9 ms |
1628 KB |
Output is correct |
24 |
Correct |
11 ms |
1832 KB |
Output is correct |
25 |
Correct |
8 ms |
1752 KB |
Output is correct |
26 |
Correct |
10 ms |
1624 KB |
Output is correct |
27 |
Correct |
9 ms |
1628 KB |
Output is correct |
28 |
Correct |
427 ms |
10972 KB |
Output is correct |
29 |
Correct |
87 ms |
3008 KB |
Output is correct |
30 |
Correct |
1969 ms |
13728 KB |
Output is correct |
31 |
Correct |
2406 ms |
17196 KB |
Output is correct |
32 |
Correct |
145 ms |
2388 KB |
Output is correct |
33 |
Correct |
845 ms |
5556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
344 KB |
Output is correct |
3 |
Correct |
4 ms |
344 KB |
Output is correct |
4 |
Correct |
3 ms |
344 KB |
Output is correct |
5 |
Correct |
3 ms |
508 KB |
Output is correct |
6 |
Correct |
76 ms |
860 KB |
Output is correct |
7 |
Correct |
54 ms |
860 KB |
Output is correct |
8 |
Correct |
307 ms |
1432 KB |
Output is correct |
9 |
Correct |
342 ms |
1308 KB |
Output is correct |
10 |
Correct |
351 ms |
1360 KB |
Output is correct |
11 |
Correct |
487 ms |
1708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
448 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
416 KB |
Output is correct |
14 |
Correct |
1 ms |
500 KB |
Output is correct |
15 |
Correct |
1 ms |
508 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
728 KB |
Output is correct |
20 |
Correct |
7 ms |
1012 KB |
Output is correct |
21 |
Correct |
6 ms |
860 KB |
Output is correct |
22 |
Correct |
9 ms |
1248 KB |
Output is correct |
23 |
Correct |
9 ms |
1628 KB |
Output is correct |
24 |
Correct |
11 ms |
1832 KB |
Output is correct |
25 |
Correct |
8 ms |
1752 KB |
Output is correct |
26 |
Correct |
10 ms |
1624 KB |
Output is correct |
27 |
Correct |
9 ms |
1628 KB |
Output is correct |
28 |
Correct |
427 ms |
10972 KB |
Output is correct |
29 |
Correct |
87 ms |
3008 KB |
Output is correct |
30 |
Correct |
1969 ms |
13728 KB |
Output is correct |
31 |
Correct |
2406 ms |
17196 KB |
Output is correct |
32 |
Correct |
145 ms |
2388 KB |
Output is correct |
33 |
Correct |
845 ms |
5556 KB |
Output is correct |
34 |
Correct |
4 ms |
344 KB |
Output is correct |
35 |
Correct |
3 ms |
344 KB |
Output is correct |
36 |
Correct |
4 ms |
344 KB |
Output is correct |
37 |
Correct |
3 ms |
344 KB |
Output is correct |
38 |
Correct |
3 ms |
508 KB |
Output is correct |
39 |
Correct |
76 ms |
860 KB |
Output is correct |
40 |
Correct |
54 ms |
860 KB |
Output is correct |
41 |
Correct |
307 ms |
1432 KB |
Output is correct |
42 |
Correct |
342 ms |
1308 KB |
Output is correct |
43 |
Correct |
351 ms |
1360 KB |
Output is correct |
44 |
Correct |
487 ms |
1708 KB |
Output is correct |
45 |
Correct |
29 ms |
1820 KB |
Output is correct |
46 |
Correct |
49 ms |
1820 KB |
Output is correct |
47 |
Correct |
40 ms |
1636 KB |
Output is correct |
48 |
Correct |
31 ms |
1820 KB |
Output is correct |
49 |
Correct |
33 ms |
1628 KB |
Output is correct |
50 |
Execution timed out |
5098 ms |
14152 KB |
Time limit exceeded |
51 |
Halted |
0 ms |
0 KB |
- |