#include <bits/stdc++.h>
using namespace std;
#define all(s) s.begin(), s.end()
const int N = 5050;
int a[N], b[N], n, dp[N][N], L[N], R[N];
vector<int> e[N];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> b[i];
a[0] = a[n + 1] = 2e9;
for (int i = 1; i <= n; ++i) {
L[i] = R[i] = i;
while (a[L[i]] <= a[i]) --L[i];
while (a[R[i]] <= a[i]) ++R[i];
}
vector<int> Ranking(a + 1, a + n + 1);
sort(all(Ranking));
Ranking.resize(unique(all(Ranking)) - Ranking.begin());
for (int i = 1; i <= n; ++i) {
a[i] = lower_bound(all(Ranking), a[i]) - Ranking.begin();
int temp_b = lower_bound(all(Ranking), b[i]) - Ranking.begin();
if (temp_b != Ranking.size() && Ranking[temp_b] != b[i]) temp_b = Ranking.size();
b[i] = temp_b;
e[b[i]].push_back(i);
}
for (int i = 1; i <= n; ++i) {
int temp = 1;
int p = 0, cnt = 0;
for (int j : e[a[i]]) if (L[i] < j && j < R[i]) {
++cnt;
if (dp[i - 1][j - 1] + 1 > dp[i - 1][p] + cnt) { p = j - 1; cnt = 1; }
dp[i][j] = max(dp[i][j], dp[i - 1][p] + cnt);
}
for (int j = 1; j <= n; ++j) dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
}
cout << dp[n][n] << '\n';
}
Compilation message
exam.cpp: In function 'int main()':
exam.cpp:25:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | if (temp_b != Ranking.size() && Ranking[temp_b] != b[i]) temp_b = Ranking.size();
| ~~~~~~~^~~~~~~~~~~~~~~~~
exam.cpp:31:13: warning: unused variable 'temp' [-Wunused-variable]
31 | int temp = 1;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
412 KB |
Output is correct |
2 |
Correct |
0 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
412 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8404 KB |
Output is correct |
2 |
Runtime error |
5 ms |
844 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
460 KB |
Output is correct |
2 |
Correct |
3 ms |
3404 KB |
Output is correct |
3 |
Correct |
22 ms |
24152 KB |
Output is correct |
4 |
Correct |
94 ms |
95472 KB |
Output is correct |
5 |
Correct |
95 ms |
99412 KB |
Output is correct |
6 |
Correct |
93 ms |
99524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
91 ms |
99488 KB |
Output is correct |
2 |
Runtime error |
4 ms |
864 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
412 KB |
Output is correct |
2 |
Correct |
0 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
412 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
1 ms |
588 KB |
Output is correct |
9 |
Correct |
1 ms |
844 KB |
Output is correct |
10 |
Correct |
2 ms |
1356 KB |
Output is correct |
11 |
Correct |
1 ms |
1312 KB |
Output is correct |
12 |
Correct |
1 ms |
1312 KB |
Output is correct |
13 |
Correct |
1 ms |
1312 KB |
Output is correct |
14 |
Correct |
1 ms |
1356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
412 KB |
Output is correct |
2 |
Correct |
0 ms |
416 KB |
Output is correct |
3 |
Correct |
1 ms |
412 KB |
Output is correct |
4 |
Correct |
1 ms |
408 KB |
Output is correct |
5 |
Correct |
1 ms |
460 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
460 KB |
Output is correct |
8 |
Correct |
3 ms |
3404 KB |
Output is correct |
9 |
Correct |
22 ms |
24152 KB |
Output is correct |
10 |
Correct |
94 ms |
95472 KB |
Output is correct |
11 |
Correct |
95 ms |
99412 KB |
Output is correct |
12 |
Correct |
93 ms |
99524 KB |
Output is correct |
13 |
Correct |
1 ms |
460 KB |
Output is correct |
14 |
Correct |
1 ms |
588 KB |
Output is correct |
15 |
Correct |
1 ms |
844 KB |
Output is correct |
16 |
Correct |
2 ms |
1356 KB |
Output is correct |
17 |
Correct |
1 ms |
1312 KB |
Output is correct |
18 |
Correct |
1 ms |
1312 KB |
Output is correct |
19 |
Correct |
1 ms |
1312 KB |
Output is correct |
20 |
Correct |
1 ms |
1356 KB |
Output is correct |
21 |
Correct |
1 ms |
1356 KB |
Output is correct |
22 |
Correct |
10 ms |
8356 KB |
Output is correct |
23 |
Correct |
205 ms |
99508 KB |
Output is correct |
24 |
Correct |
110 ms |
99328 KB |
Output is correct |
25 |
Correct |
93 ms |
99464 KB |
Output is correct |
26 |
Correct |
99 ms |
99392 KB |
Output is correct |
27 |
Correct |
99 ms |
99400 KB |
Output is correct |
28 |
Correct |
95 ms |
99492 KB |
Output is correct |
29 |
Correct |
92 ms |
99364 KB |
Output is correct |
30 |
Correct |
122 ms |
99396 KB |
Output is correct |
31 |
Correct |
94 ms |
99384 KB |
Output is correct |
32 |
Correct |
95 ms |
99420 KB |
Output is correct |