#include<bits/stdc++.h>
#define taskname "C"
using namespace std;
template<class T>void maximize(T& a, T b){
if(a < b){
a = b;
}
}
const int lim = 1e5 + 5;
int n, N, a[lim], b[lim];
vector<int>compress;
namespace sub2{
void solve(){
int ans = 0;
for(int i = 1; i <= n; i++){
if(a[i] > b[1]){
continue;
}
if(a[i] == b[1]){
int l = i;
ans++;
while(--l > 0 && a[l] <= b[1]){
ans++;
}
while(++i <= n && a[i] <= b[1]){
ans++;
}
}
}
cout << ans;
}
}
namespace sub3{
const int lim = 5e3 + 5;
int dp[2][lim << 1];
bitset<lim << 1>app;
void solve(){
app.reset();
for(int i = 1; i <= n; i++){
app.set(a[i]);
}
memset(dp, 0, sizeof(dp));
bool cur = true, pre = false;
for(int i = 1; i <= n; i++){
swap(cur, pre);
fill(dp[cur], dp[cur] + a[i], 0);
for(int j = a[i]; j <= N; j++){
dp[cur][j] = max(dp[cur][j - 1], (app.test(j) ? dp[pre][j] + int(j == b[i]) : 0));
}
}
cout << dp[cur][N];
}
}
namespace sub4{
int p[lim << 1], bit[lim << 1], left[lim], right[lim];
void update(int p, int x){
for(; p <= N; p += p & -p){
maximize(bit[p], x);
}
}
int get(int p){
int ans = 0;
for(; p > 0; p -= p & -p){
maximize(ans, bit[p]);
}
return ans;
}
void solve(){
memset(p, -1, sizeof(p));
stack<int>st;
for(int i = 1; i <= n; st.push(i++)){
p[a[i]] = i;
while(!st.empty() && a[i] > a[st.top()]){
st.pop();
}
left[i] = (st.empty() ? 1 : st.top() + 1);
}
while(!st.empty()){
st.pop();
}
for(int i = n; i > 0; st.push(i--)){
while(!st.empty() && a[i] > a[st.top()]){
st.pop();
}
right[i] = (st.empty() ? n : st.top() - 1);
}
memset(bit, 0, sizeof(bit));
for(int i = 1; i <= n; i++){
if(p[b[i]] != -1 && left[p[b[i]]] <= i && right[p[b[i]]] >= i){
update(p[b[i]], get(p[b[i]]) + 1);
}
}
cout << get(N);
}
}
namespace sub156{
const int lim = 5e3 + 5;
int dp[lim], left[lim], right[lim];
void solve(){
stack<int>st;
for(int i = 1; i <= n; st.push(i++)){
while(!st.empty() && a[i] > a[st.top()]){
st.pop();
}
left[i] = (st.empty() ? 1 : st.top() + 1);
}
while(!st.empty()){
st.pop();
}
for(int i = n; i > 0; st.push(i--)){
while(!st.empty() && a[i] > a[st.top()]){
st.pop();
}
right[i] = (st.empty() ? n : st.top() - 1);
}
memset(dp, 0, sizeof(dp));
for(int i = 1; i <= n; i++){
for(int j = 1; j <= n; j++){
maximize(dp[j], dp[j - 1] + int(a[i] == b[j] && left[i] <= j && right[i] >= j));
}
}
cout << dp[n];
}
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(taskname".inp", "r")){
freopen(taskname".inp", "r", stdin);
}
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
compress.emplace_back(a[i]);
}
for(int i = 1; i <= n; i++){
cin >> b[i];
compress.emplace_back(b[i]);
}
sort(compress.begin(), compress.end());
compress.resize(unique(compress.begin(), compress.end()) - compress.begin());
for(int i = 1; i <= n; i++){
a[i] = lower_bound(compress.begin(), compress.end(), a[i]) - compress.begin() + 1;
b[i] = lower_bound(compress.begin(), compress.end(), b[i]) - compress.begin() + 1;
}
N = compress.size();
if(*min_element(b + 1, b + n + 1) == *max_element(b + 1, b + n + 1)){
sub2::solve();
}
else if(n <= 5000){
bool is_sub3 = true;
for(int i = 2; i <= n; i++){
if(a[i] < a[i - 1]){
is_sub3 = false;
break;
}
}
if(is_sub3){
sub3::solve();
}
else{
sub156::solve();
}
}
else{
sub4::solve();
}
}
Compilation message (stderr)
exam.cpp: In function 'int main()':
exam.cpp:128:24: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
128 | freopen(taskname".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |