#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <deque>
#include <stack>
#include <iomanip>
#include <unordered_set>
using namespace std;
#define ll long long
#define ff first
#define ss second
ll n, k, sum, m, s, f;
ll MOD = 1e9 + 7;
ll INF = 1e18 + 7;
ll ttt;
ll a[200007];
ll b[200007];
ll dp[200007];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
bool ent2 = true;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> b[i];
if (b[0] != b[i]) {
ent2 = false;
}
}
if (ent2) {
ll ans = 0, cnt = 0, cur = 0;
bool p = false, q = false;
for (int i = 0; i < n; i++) {
if (a[i] < b[0]) {
if (p) {
cnt++;
}
cur++;
}
else if (a[i] == b[0]) {
cnt = ++cur;
p = true;
}
else {
ans += cnt;
cnt = 0, cur = 0;
p = false;
}
}
cout << ans + cnt << endl;
}
else {
for (int i = 0; i < n; i++) {
ll cnt = 0;
vector<ll>s;
for (int j = i; j >= 0; j--) {
if (b[j] == a[i]) {
cnt++;
s.push_back(j);
}
}
dp[i + 1] = max(cnt, dp[i]);
for (int j = 0; j < s.size(); j++) {
dp[i + 1] = max(dp[i + 1], dp[s[j]] + j + 1);
}
}
/*for (int i = 0; i <= n; i++) {
cout << i << " " << dp[i] << endl;
}*/
cout << dp[n] << endl;
}
return 0;
}
Compilation message
exam.cpp: In function 'int main()':
exam.cpp:44:19: warning: unused variable 'q' [-Wunused-variable]
44 | bool p = false, q = false;
| ^
exam.cpp:75:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | for (int j = 0; j < s.size(); j++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
6 ms |
636 KB |
Output is correct |
3 |
Correct |
19 ms |
1776 KB |
Output is correct |
4 |
Correct |
15 ms |
1996 KB |
Output is correct |
5 |
Correct |
28 ms |
2032 KB |
Output is correct |
6 |
Correct |
16 ms |
1916 KB |
Output is correct |
7 |
Correct |
17 ms |
1964 KB |
Output is correct |
8 |
Correct |
33 ms |
1888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Incorrect |
0 ms |
332 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |