Links: https://ac.nowcoder.com/acm/contest/881/A
Source: Cattle-off network
Equivalent Prefixes
time limit: C / C ++ 2 seconds, and other languages 4 seconds
space limitations: C / C ++ 524288K, other languages 1048576K
64bit the IO the Format:% LLD
Title Description
Two arrays u and v each with m distinct elements are called equivalent if and only IF
R & lt
m
Q
(
U
,
L
,
R & lt
)
=
R & lt
m
Q
(
V
,
L
,
R & lt
)
RMQ (U, L, R & lt) RMQ = (V, L, R & lt) for All
. 1
≤
L
≤
R & lt
≤
m
1 ≦ l≤r≤m
WHERE
R & lt
M
Q
(
W
,
l
,
r
)
RMQ(w,l,r) denotes the index of the minimum element among
w
l
,
w
l
- 1
,
…
,
w
r
wl,wl+1,…,wr.
Since the array contains distinct elements, the definition of minimum is unambiguous.
Bobo has two arrays a and b each with n distinct elements. Find the maximum number
p
≤
n
p≤n where
{
a
1
,
a
2
,
…
,
a
p
}
{a1,a2,…,ap} and
{
b
1
,
b
2
,
…
,
b
p
}
{b1,b2,…,bp} are equivalent.
输入描述:
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains an integer n.
The second line contains n integers
a
1
,
a
2
,
…
,
a
n
a1,a2,…,an.
The third line contains n integers
b
1
,
b
2
,
…
,
b
n
b1,b2,…,bn.
- 1
≤
n
≤
10
5
1≤n≤105 - 1
≤
a
i
,
b
i
≤
n
1≤ai,bi≤n - {
a
1
,
a
2
,
…
,
a
n
}
{a1,a2,…,an} are distinct. - {
b
1
,
b
2
,
…
,
b
n
}
{b1,b2,…,bn} are distinct. - Of The SUM of n-does Not Exceed
. 5
×
10
. 5
. 5 × 105.
Output Description:
the For each Test Case, Print AN Integer Which Denotes The Result.
Example 1
Input
Copy
2
1 2
2 1
. 3
2 1. 3
. 3 1 2
. 5
. 3 1 2. 4. 5
. 5. 4. 3 2. 1
output
copy
. 1
. 3
. 4
Question is intended:
to give you two arrays a, b, of size n, let you find a number p (1 <= p <= n), so that the minimum value of a, b 1 ~ p array of any one section the same standard.
Ideas:
P is easy to know the value monotonic, we first used in a pretreatment st table array a, b, to facilitate subsequent RMQ, because the number in the array different from each other, then we can get directly RMQ minimum interval subscript .
In the process of bipartite p, we judge this mid legality:
1 ~ mid section asks if two minimum values in the array index, and if not directly returns false, otherwise, the minimum value of the subscript minid demarcation point recursive process 1 ~ minid, minid + 1 ~ mid. This process is O (n) of
Therefore, the overall time complexity of O (n * log n)
See details Code:
#include<iostream>
using namespace std;
const int maxn=2e5+10;
int n;
int a[maxn],b[maxn];
int sa[maxn][20],sb[maxn][20],mn[maxn];
void init()
{
mn[0]=-1;
for (int i=1;i<=n;i++)
{
mn[i]=((i & (i-1))==0) ? mn[i-1]+1 : mn[i-1];
sa[i][0]=a[i];
sb[i][0]=b[i];
}
for (int j=1;j<=mn[n];j++)
for (int i=1;i+(1<<j)-1<=n;i++)
{
sa[i][j]=min(sa[i][j-1],sa[i+(1<<(j-1))][j-1]);
sb[i][j]=min(sb[i][j-1],sb[i+(1<<(j-1))][j-1]);
}
}
int ida[maxn];
int idb[maxn];
int rqa(int L,int R)
{
int k=mn[R-L+1];
// cout<<"a "<<min(sa[L][k],sa[R-(1<<k)+1][k])<<endl;
return ida[min(sa[L][k],sa[R-(1<<k)+1][k])];
}
int rqb(int L,int R)
{
int k=mn[R-L+1];
// cout<<"b "<<min(sb[L][k],sb[R-(1<<k)+1][k])<<endl;
return idb[min(sb[L][k],sb[R-(1<<k)+1][k])];
}
bool pan(int l,int r)
{
if(l>=r)
{
return 1;
}
int w=rqb(l,r);
int q=rqa(l,r);
if(w!=q)
{
return 0;
}else{
return pan(l,w-1)&&(pan(q+1,r));
}
}
bool check(int mid)
{
// bool res=1;
return pan(1,mid);
}
int main(){
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
ida[a[i]]=i;
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
idb[b[i]]=i;;
}
init();
int l=1;
int r=n;
int mid;
int ans=1;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid))
{
l=mid+1;
ans=mid;
}else{
r=mid-1;
}
}
printf("%d\n",ans);
}
}