#include <bits/stdc++.h>
int main() {
using namespace std;
ios_base::sync_with_stdio(false), cin.tie(nullptr);
int N; cin >> N;
vector<int> A(N); for (auto& a : A) cin >> a;
vector<int> B(N); for (auto& b : B) cin >> b;
vector<array<int, 2>> match(N, array<int, 2>{-1, -1});
for (int z = 0; z < 2; z++) {
unordered_map<int, int> in_stack;
vector<int> stk; stk.reserve(N);
for (int j = 0; j < N; j++) {
int i = z ? N-1-j : j;
while (!stk.empty() && stk.back() <= A[i]) {
in_stack.erase(stk.back());
stk.pop_back();
}
stk.push_back(A[i]);
in_stack[A[i]] = i;
if (in_stack.count(B[i])) {
match[i][z] = in_stack[B[i]];
}
}
}
vector<int> V; V.reserve(2*N);
for (int i = 0; i < N; i++) {
if (match[i][1] != -1) {
V.push_back(match[i][1]);
}
if (match[i][0] != -1 && match[i][0] != match[i][1]) {
V.push_back(match[i][0]);
}
}
vector<int> best; best.reserve(V.size());
for (int v : V) {
int mi = -1;
int ma = int(best.size());
while (ma - mi > 1) {
int md = (mi + ma) / 2;
if (v >= best[md]) mi = md;
else ma = md;
}
if (ma == int(best.size())) best.push_back(v);
else best[ma] = v;
}
cout << best.size() << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
11 ms |
1172 KB |
Output is correct |
3 |
Correct |
33 ms |
7772 KB |
Output is correct |
4 |
Correct |
41 ms |
3416 KB |
Output is correct |
5 |
Correct |
48 ms |
4692 KB |
Output is correct |
6 |
Correct |
24 ms |
2544 KB |
Output is correct |
7 |
Correct |
39 ms |
3576 KB |
Output is correct |
8 |
Correct |
36 ms |
3840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
588 KB |
Output is correct |
5 |
Correct |
2 ms |
596 KB |
Output is correct |
6 |
Correct |
3 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
15 ms |
1680 KB |
Output is correct |
3 |
Correct |
32 ms |
3028 KB |
Output is correct |
4 |
Correct |
40 ms |
3824 KB |
Output is correct |
5 |
Correct |
60 ms |
8784 KB |
Output is correct |
6 |
Correct |
45 ms |
5708 KB |
Output is correct |
7 |
Correct |
48 ms |
6512 KB |
Output is correct |
8 |
Correct |
31 ms |
3052 KB |
Output is correct |
9 |
Correct |
37 ms |
3428 KB |
Output is correct |
10 |
Correct |
39 ms |
5464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
324 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
2 ms |
588 KB |
Output is correct |
11 |
Correct |
2 ms |
596 KB |
Output is correct |
12 |
Correct |
3 ms |
596 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
324 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
328 KB |
Output is correct |
22 |
Correct |
1 ms |
324 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
372 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
3 ms |
464 KB |
Output is correct |
29 |
Correct |
2 ms |
584 KB |
Output is correct |
30 |
Correct |
2 ms |
596 KB |
Output is correct |
31 |
Correct |
2 ms |
468 KB |
Output is correct |
32 |
Correct |
3 ms |
572 KB |
Output is correct |