#include <bits/stdc++.h>
using namespace std;
int n;
int a[100005],b[100005],aux[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);
}
}
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()
{
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();
cout<<solve()<<'\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 |
4940 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 |
3 ms |
5068 KB |
Output is correct |
2 |
Correct |
81 ms |
5704 KB |
Output is correct |
3 |
Correct |
41 ms |
9972 KB |
Output is correct |
4 |
Correct |
38 ms |
8300 KB |
Output is correct |
5 |
Correct |
78 ms |
10444 KB |
Output is correct |
6 |
Execution timed out |
1091 ms |
8476 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4940 KB |
Output is correct |
2 |
Correct |
4 ms |
5068 KB |
Output is correct |
3 |
Correct |
4 ms |
5068 KB |
Output is correct |
4 |
Correct |
7 ms |
5224 KB |
Output is correct |
5 |
Correct |
7 ms |
5324 KB |
Output is correct |
6 |
Correct |
7 ms |
5328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
5324 KB |
Output is correct |
2 |
Correct |
42 ms |
7360 KB |
Output is correct |
3 |
Correct |
102 ms |
10540 KB |
Output is correct |
4 |
Correct |
97 ms |
10684 KB |
Output is correct |
5 |
Correct |
88 ms |
10812 KB |
Output is correct |
6 |
Correct |
85 ms |
10808 KB |
Output is correct |
7 |
Correct |
82 ms |
10808 KB |
Output is correct |
8 |
Correct |
93 ms |
10716 KB |
Output is correct |
9 |
Correct |
102 ms |
10832 KB |
Output is correct |
10 |
Correct |
75 ms |
10812 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 |
4940 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 |
4 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 |
4964 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 |
4940 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 |
4 ms |
5068 KB |
Output is correct |
9 |
Correct |
4 ms |
5068 KB |
Output is correct |
10 |
Correct |
7 ms |
5224 KB |
Output is correct |
11 |
Correct |
7 ms |
5324 KB |
Output is correct |
12 |
Correct |
7 ms |
5328 KB |
Output is correct |
13 |
Correct |
3 ms |
4940 KB |
Output is correct |
14 |
Correct |
4 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 |
4964 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 |
4 ms |
4940 KB |
Output is correct |
22 |
Correct |
3 ms |
5068 KB |
Output is correct |
23 |
Correct |
39 ms |
5268 KB |
Output is correct |
24 |
Correct |
20 ms |
5240 KB |
Output is correct |
25 |
Correct |
8 ms |
5196 KB |
Output is correct |
26 |
Correct |
6 ms |
5196 KB |
Output is correct |
27 |
Correct |
9 ms |
5196 KB |
Output is correct |
28 |
Correct |
8 ms |
5228 KB |
Output is correct |
29 |
Correct |
7 ms |
5204 KB |
Output is correct |
30 |
Correct |
6 ms |
5196 KB |
Output is correct |
31 |
Correct |
6 ms |
5180 KB |
Output is correct |
32 |
Correct |
6 ms |
5184 KB |
Output is correct |