#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <deque>
#include <map>
#include <set>
#include <complex>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <chrono>
#define ft first
#define sc second
#define pb push_back
#define len(v) (int)v.size()
#define int ll
using namespace std;
typedef long long ll;
typedef long double ld;
map<pair<int, int>, int> dp;
int w, h;
vector<int> a;
vector<int> b;
int solve(int i, int j) {
// cout << a[i] << " " << b[j] << endl;
if(dp.count({i, j}))
return dp[{i, j}];
if(a[i] > b[j]) {
int r = w;
for (int now = j; now < w; now++) {
if(b[now] > a[i]) {
r = now;
break;
}
}
if(r == w)
dp[{i, j}] = max(dp[{i, j}], w - j);
else
dp[{i, j}] = max(dp[{i, j}], r - j + solve(i, r));
int l = -1;
for (int now = j; now >= 0; now--) {
if(b[now] > a[i]) {
l = now;
break;
}
}
if(l == -1)
dp[{i, j}] = max(dp[{i, j}], j + 1);
else
dp[{i, j}] = max(dp[{i, j}], j - l + solve(i, l));
}
else {
int r = h;
for (int now = i; now < h; now++) {
if(a[now] > b[j]) {
r = now;
break;
}
}
if(r == h)
dp[{i, j}] = max(dp[{i, j}], h - i);
else
dp[{i, j}] = max(dp[{i, j}], r - i + solve(r, j));
int l = -1;
for (int now = i; now >= 0; now--) {
if(a[now] > b[j]) {
l = now;
break;
}
}
if(l == -1)
dp[{i, j}] = max(dp[{i, j}], i + 1);
else
dp[{i, j}] = max(dp[{i, j}], i - l + solve(l, j));
}
return dp[{i, j}];
}
signed main() {
#ifdef PC
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int q;
cin >> h >> w >> q;
a = vector<int> (h);
b = vector<int> (w);
for (int i = 0; i < h; i++) {
cin >> a[i];
}
for (int j = 0; j < w; j++) {
cin >> b[j];
}
for (int k = 0; k < q; k++) {
int i, j;
cin >> i >> j;
i--, j--;
int ans = 1;
for (int j1 = j + 1; j1 < w; j1++) {
ans = max(ans, j1 - j + 1);
if(b[j1] > a[i]) {
ans = max(ans, j1 - j + solve(i, j1));
break;
}
}
for (int j1 = j - 1; j1 >= 0; j1--) {
ans = max(ans, j - j1 + 1);
if(b[j1] > a[i]) {
ans = max(ans, j - j1 + solve(i, j1));
break;
}
}
for (int i1 = i + 1; i1 < h; i1++) {
ans = max(ans, i1 - i + 1);
if(a[i1] > b[j]) {
ans = max(ans, i1 - i + solve(i1, j));
break;
}
}
for (int i1 = i - 1; i1 >= 0; i1--) {
ans = max(ans, i - i1 + 1);
if(a[i1] > b[j]) {
ans = max(ans, i - i1 + solve(i1, j));
break;
}
}
cout << ans - 1 << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
376 KB |
Output is correct |
19 |
Correct |
11 ms |
504 KB |
Output is correct |
20 |
Correct |
13 ms |
636 KB |
Output is correct |
21 |
Correct |
11 ms |
632 KB |
Output is correct |
22 |
Correct |
16 ms |
760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
376 KB |
Output is correct |
19 |
Correct |
11 ms |
504 KB |
Output is correct |
20 |
Correct |
13 ms |
636 KB |
Output is correct |
21 |
Correct |
11 ms |
632 KB |
Output is correct |
22 |
Correct |
16 ms |
760 KB |
Output is correct |
23 |
Correct |
19 ms |
1144 KB |
Output is correct |
24 |
Correct |
19 ms |
1144 KB |
Output is correct |
25 |
Correct |
19 ms |
1144 KB |
Output is correct |
26 |
Correct |
20 ms |
1144 KB |
Output is correct |
27 |
Correct |
20 ms |
1144 KB |
Output is correct |
28 |
Correct |
1012 ms |
5264 KB |
Output is correct |
29 |
Correct |
244 ms |
1808 KB |
Output is correct |
30 |
Correct |
2292 ms |
6932 KB |
Output is correct |
31 |
Correct |
2815 ms |
8636 KB |
Output is correct |
32 |
Correct |
183 ms |
1528 KB |
Output is correct |
33 |
Correct |
920 ms |
2928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
504 KB |
Output is correct |
2 |
Correct |
8 ms |
504 KB |
Output is correct |
3 |
Correct |
8 ms |
504 KB |
Output is correct |
4 |
Correct |
8 ms |
504 KB |
Output is correct |
5 |
Correct |
8 ms |
504 KB |
Output is correct |
6 |
Correct |
8 ms |
504 KB |
Output is correct |
7 |
Correct |
11 ms |
632 KB |
Output is correct |
8 |
Correct |
17 ms |
760 KB |
Output is correct |
9 |
Correct |
16 ms |
760 KB |
Output is correct |
10 |
Correct |
22 ms |
760 KB |
Output is correct |
11 |
Correct |
19 ms |
892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
380 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
6 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
5 ms |
376 KB |
Output is correct |
11 |
Correct |
5 ms |
376 KB |
Output is correct |
12 |
Correct |
6 ms |
376 KB |
Output is correct |
13 |
Correct |
6 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
376 KB |
Output is correct |
15 |
Correct |
5 ms |
376 KB |
Output is correct |
16 |
Correct |
6 ms |
376 KB |
Output is correct |
17 |
Correct |
6 ms |
376 KB |
Output is correct |
18 |
Correct |
6 ms |
376 KB |
Output is correct |
19 |
Correct |
11 ms |
504 KB |
Output is correct |
20 |
Correct |
13 ms |
636 KB |
Output is correct |
21 |
Correct |
11 ms |
632 KB |
Output is correct |
22 |
Correct |
16 ms |
760 KB |
Output is correct |
23 |
Correct |
19 ms |
1144 KB |
Output is correct |
24 |
Correct |
19 ms |
1144 KB |
Output is correct |
25 |
Correct |
19 ms |
1144 KB |
Output is correct |
26 |
Correct |
20 ms |
1144 KB |
Output is correct |
27 |
Correct |
20 ms |
1144 KB |
Output is correct |
28 |
Correct |
1012 ms |
5264 KB |
Output is correct |
29 |
Correct |
244 ms |
1808 KB |
Output is correct |
30 |
Correct |
2292 ms |
6932 KB |
Output is correct |
31 |
Correct |
2815 ms |
8636 KB |
Output is correct |
32 |
Correct |
183 ms |
1528 KB |
Output is correct |
33 |
Correct |
920 ms |
2928 KB |
Output is correct |
34 |
Correct |
8 ms |
504 KB |
Output is correct |
35 |
Correct |
8 ms |
504 KB |
Output is correct |
36 |
Correct |
8 ms |
504 KB |
Output is correct |
37 |
Correct |
8 ms |
504 KB |
Output is correct |
38 |
Correct |
8 ms |
504 KB |
Output is correct |
39 |
Correct |
8 ms |
504 KB |
Output is correct |
40 |
Correct |
11 ms |
632 KB |
Output is correct |
41 |
Correct |
17 ms |
760 KB |
Output is correct |
42 |
Correct |
16 ms |
760 KB |
Output is correct |
43 |
Correct |
22 ms |
760 KB |
Output is correct |
44 |
Correct |
19 ms |
892 KB |
Output is correct |
45 |
Correct |
38 ms |
1400 KB |
Output is correct |
46 |
Correct |
35 ms |
1400 KB |
Output is correct |
47 |
Correct |
31 ms |
1404 KB |
Output is correct |
48 |
Correct |
36 ms |
1400 KB |
Output is correct |
49 |
Correct |
38 ms |
1400 KB |
Output is correct |
50 |
Correct |
1060 ms |
5248 KB |
Output is correct |
51 |
Correct |
1134 ms |
5636 KB |
Output is correct |
52 |
Correct |
3163 ms |
11000 KB |
Output is correct |
53 |
Correct |
3252 ms |
10800 KB |
Output is correct |
54 |
Correct |
3178 ms |
10192 KB |
Output is correct |
55 |
Correct |
4528 ms |
15536 KB |
Output is correct |
56 |
Execution timed out |
5008 ms |
92020 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |