#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
int n; cin >> n;
vector<int> a(n), b(n);
for (int &x : a) {
cin >> x;
}
for (int &x : b) {
cin >> x;
}
if (n > 5000) {
int res = 0;
for (int i = 0; i < n; ++i) {
if (a[i] > b[0]) {
continue;
}
int j = i - 1;
bool flg = 0;
while (j + 1 < n && a[j + 1] <= b[0]) {
flg |= a[++j] == b[0];
}
res += flg * (j - i + 1);
i = j;
}
cout << res;
exit(0);
}
vector<int> comp;
for (int i = 0; i < n; ++i) {
comp.push_back(a[i]);
comp.push_back(b[i]);
}
sort(comp.begin(), comp.end());
comp.erase(unique(comp.begin(), comp.end()), comp.end());
for (int i = 0; i < n; ++i) {
a[i] = lower_bound(comp.begin(), comp.end(), a[i]) - comp.begin();
b[i] = lower_bound(comp.begin(), comp.end(), b[i]) - comp.begin();
}
int m = comp.size();
vector<int> dp(n + 1), cnt(m);
dp[0] = 0;
for (int i = 1; i <= n; ++i) {
dp[i] = dp[i - 1];
int ma = 0;
for (int j = i - 1; j >= 0; --j) {
ma = max(ma, a[j]);
++cnt[b[j]];
dp[i] = max(dp[i], dp[j] + cnt[ma]);
}
for (int j = 0; j < i; ++j) {
cnt[b[j]] = 0;
}
}
cout << dp.back();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Correct |
3 ms |
1000 KB |
Output is correct |
3 |
Correct |
9 ms |
2140 KB |
Output is correct |
4 |
Correct |
7 ms |
1628 KB |
Output is correct |
5 |
Correct |
14 ms |
3160 KB |
Output is correct |
6 |
Correct |
8 ms |
1628 KB |
Output is correct |
7 |
Correct |
8 ms |
1620 KB |
Output is correct |
8 |
Correct |
15 ms |
3032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |