链接:https://codeforces.com/contest/1237/problem/B
Consider a tunnel on a one-way road. During a particular day, nn cars numbered from 11 to nn entered and exited the tunnel exactly once. All the cars passed through the tunnel at constant speeds.
A traffic enforcement camera is mounted at the tunnel entrance. Another traffic enforcement camera is mounted at the tunnel exit. Perfectly balanced.
Thanks to the cameras, the order in which the cars entered and exited the tunnel is known. No two cars entered or exited at the same time.
Traffic regulations prohibit overtaking inside the tunnel. If car ii overtakes any other car jj inside the tunnel, car ii must be fined. However, each car can be fined at most once.
Formally, let's say that car ii definitely overtook car jj if car ii entered the tunnel later than car jj and exited the tunnel earlier than car jj. Then, car ii must be fined if and only if it definitely overtook at least one other car.
Find the number of cars that must be fined.
Input
The first line contains a single integer nn (2≤n≤1052≤n≤105), denoting the number of cars.
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n), denoting the ids of cars in order of entering the tunnel. All aiai are pairwise distinct.
The third line contains nn integers b1,b2,…,bnb1,b2,…,bn (1≤bi≤n1≤bi≤n), denoting the ids of cars in order of exiting the tunnel. All bibi are pairwise distinct.
Output
Output the number of cars to be fined.
Examples
input
Copy
5 3 5 2 1 4 4 3 2 5 1
output
Copy
2
input
Copy
7 5 2 3 6 7 1 4 2 3 6 7 1 4 5
output
Copy
6
input
Copy
2 1 2 1 2
output
Copy
0
Note
The first example is depicted below:
Car 22 definitely overtook car 55, while car 44 definitely overtook cars 11, 22, 33 and 55. Cars 22 and 44 must be fined.
In the second example car 55 was definitely overtaken by all other cars.
In the third example no car must be fined.
题解:一开始上来觉得线段树啊,突然一想B题怎么可能线段树,于是开始找On解法,后来仔细一想只需要从后往前找找到每辆车是否超车就行
#include<bits/stdc++.h>
using namespace std;
long long n,t;
long long s1=0,s2=0;
struct node{
int id;
int f;
int l;
int d;
}x[1000001];
bool cmp(node p,node q)
{
return p.l<q.l;
}
int main()
{
cin>>n;
long long u;
for(int i=1;i<=n;i++)
{
cin>>u;
x[u].f=i;
x[u].id=u;
}
for(int i=1;i<=n;i++)
{
cin>>u;
x[u].l=i;
x[u].d=x[u].l-x[u].f;
}
sort(x+1,x+1+n,cmp);
int s=0,s1=0,k=x[n].f;
for(int i=n-1;i>=1;i--)
{
if(x[i].f>k)
{
s++;
}
else
{
k=x[i].f;
}
}
cout<<s;
}