#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005],b[100005],l[100005],r[100005],fr[100005];
vector<int> poz[200005],v;
class arbore_indexat_binar
{
int aib[100005];
private:
int ub(int x)
{
return (x&(-x));
}
public:
void update(int poz, int val)
{
for(int i=poz;i<=n;i+=ub(i))
{
aib[i] = max(aib[i],val);
}
}
int query(int poz)
{
int rez = 0;
for(int i=poz;i>=1;i-=ub(i))
{
rez = max(rez,aib[i]);
}
return rez;
}
} aib;
void precalc_stack()
{
stack<int> st;
for(int i=1; i<=n; i++)
{
while(!st.empty() && a[i]>a[st.top()])
{
st.pop();
}
if(st.empty())
{
l[i] = 0;
}
else
{
l[i] = st.top();
}
st.push(i);
}
while(!st.empty())
{
st.pop();
}
for(int i=n; i>=1; i--)
{
while(!st.empty() && a[i]>a[st.top()])
{
st.pop();
}
if(st.empty())
{
r[i] = n+1;
}
else
{
r[i] = st.top();
}
st.push(i);
}
}
bool subtask2()
{
for(int i=2; i<=n; i++)
{
if(b[i]!=b[i-1])
{
return false;
}
}
return true;
}
int solve_subtask2()
{
for(int i=1; i<=n; i++)
{
if(a[i]==b[1])
{
++fr[l[i]+1];
--fr[r[i]];
}
}
int rez = 0;
for(int i=1; i<=n; i++)
{
fr[i]+=fr[i-1];
if(fr[i])
{
++rez;
}
}
return rez;
}
int solve_dp()
{
for(int i=1;i<=n;i++)
{
poz[a[i]].push_back(i);
}
int Max = 0;
for(int i=1;i<=n;i++)
{
for(int j=poz[b[i]].size()-1;j>=0;j--)
{
int p = poz[b[i]][j];
if(p>i && l[p]>=i)
{
continue;
}
if(p<i && r[p]<=i)
{
continue;
}
int dp = 1 + aib.query(p);
aib.update(p,dp);
Max = max(Max,dp);
}
}
return Max;
}
int get_poz(int val)
{
int st = 1;
int dr = v.size();
int poz = 0;
while(st<=dr)
{
int mij = (st+dr)>>1;
if(v[mij-1]<=val)
{
poz = mij;
st = mij+1;
}
else
{
dr = mij-1;
}
}
return poz;
}
int main()
{
// freopen("nr.in","r",stdin);
// freopen("nr.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n;
for(int i=1; i<=n; i++)
{
cin>>a[i];
v.push_back(a[i]);
}
for(int i=1; i<=n; i++)
{
cin>>b[i];
v.push_back(b[i]);
}
sort(v.begin(),v.end());
for(int i=1;i<=n;i++)
{
a[i] = get_poz(a[i]);
b[i] = get_poz(b[i]);
}
precalc_stack();
if(subtask2())
{
cout<<solve_subtask2()<<'\n';
return 0;
}
cout<<solve_dp()<<'\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5068 KB |
Output is correct |
2 |
Correct |
12 ms |
5724 KB |
Output is correct |
3 |
Correct |
35 ms |
7768 KB |
Output is correct |
4 |
Correct |
34 ms |
8104 KB |
Output is correct |
5 |
Correct |
64 ms |
7876 KB |
Output is correct |
6 |
Correct |
35 ms |
7868 KB |
Output is correct |
7 |
Correct |
40 ms |
7852 KB |
Output is correct |
8 |
Correct |
61 ms |
7844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
6 ms |
5308 KB |
Output is correct |
5 |
Correct |
6 ms |
5324 KB |
Output is correct |
6 |
Correct |
6 ms |
5324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5248 KB |
Output is correct |
2 |
Correct |
38 ms |
7316 KB |
Output is correct |
3 |
Correct |
92 ms |
10560 KB |
Output is correct |
4 |
Correct |
100 ms |
10668 KB |
Output is correct |
5 |
Correct |
91 ms |
10764 KB |
Output is correct |
6 |
Correct |
82 ms |
10828 KB |
Output is correct |
7 |
Correct |
82 ms |
10808 KB |
Output is correct |
8 |
Correct |
93 ms |
10816 KB |
Output is correct |
9 |
Correct |
96 ms |
10812 KB |
Output is correct |
10 |
Correct |
74 ms |
10804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
4940 KB |
Output is correct |
9 |
Correct |
3 ms |
4940 KB |
Output is correct |
10 |
Correct |
3 ms |
4940 KB |
Output is correct |
11 |
Correct |
4 ms |
4940 KB |
Output is correct |
12 |
Correct |
3 ms |
4940 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
3 ms |
4980 KB |
Output is correct |
5 |
Correct |
3 ms |
4940 KB |
Output is correct |
6 |
Correct |
3 ms |
4940 KB |
Output is correct |
7 |
Correct |
3 ms |
4940 KB |
Output is correct |
8 |
Correct |
3 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
6 ms |
5308 KB |
Output is correct |
11 |
Correct |
6 ms |
5324 KB |
Output is correct |
12 |
Correct |
6 ms |
5324 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
3 ms |
4940 KB |
Output is correct |
15 |
Correct |
3 ms |
4940 KB |
Output is correct |
16 |
Correct |
3 ms |
4940 KB |
Output is correct |
17 |
Correct |
4 ms |
4940 KB |
Output is correct |
18 |
Correct |
3 ms |
4940 KB |
Output is correct |
19 |
Correct |
3 ms |
4940 KB |
Output is correct |
20 |
Correct |
4 ms |
4940 KB |
Output is correct |
21 |
Correct |
3 ms |
4940 KB |
Output is correct |
22 |
Correct |
4 ms |
5068 KB |
Output is correct |
23 |
Correct |
6 ms |
5196 KB |
Output is correct |
24 |
Correct |
17 ms |
5232 KB |
Output is correct |
25 |
Correct |
9 ms |
5232 KB |
Output is correct |
26 |
Correct |
6 ms |
5196 KB |
Output is correct |
27 |
Correct |
7 ms |
5248 KB |
Output is correct |
28 |
Correct |
7 ms |
5196 KB |
Output is correct |
29 |
Correct |
6 ms |
5196 KB |
Output is correct |
30 |
Correct |
7 ms |
5180 KB |
Output is correct |
31 |
Correct |
6 ms |
5196 KB |
Output is correct |
32 |
Correct |
7 ms |
5196 KB |
Output is correct |