This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | 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... |