部分题解后面更行

 

A Equivalent Prefixes

题解:https://blog.csdn.net/liufengwei1/article/details/96515809

参考代码:

#include<bits/stdc++.h>
#define maxl 100010
using namespace std;
 
int n,ans,top;
int a[maxl],b[maxl];
int la[maxl],ra[maxl],lb[maxl],rb[maxl];
int s[maxl];
 
inline bool jug(int mid)
{
    top=0;
    for(int i=1;i<=mid;i++)
    {
        while(top>0 && a[i]<a[s[top]])
            ra[s[top]]=i-1,top--;
        s[++top]=i;
        la[i]=s[top-1]+1;
    }
    while(top>0) ra[s[top]]=mid,top--;
    top=0;
    for(int i=1;i<=mid;i++)
    {
        while(top>0 && b[i]<b[s[top]])
            rb[s[top]]=i-1,top--;
        s[++top]=i;
        lb[i]=s[top-1]+1;
    }
    while(top>0) rb[s[top]]=mid,top--;
    for(int i=1;i<=mid;i++)
    if(la[i]!=lb[i] || ra[i]!=rb[i]) return false;
    return true;
}
 
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++) scanf("%d",&a[i]);
        for(int i=1;i<=n;i++) scanf("%d",&b[i]);
        int l=1,r=n,mid;
        while(l+1<r)
        {
            mid=(l+r)>>1;
            if(jug(mid)) l=mid;
            else r=mid; 
        }
        if(jug(r)) ans=r;
        else ans=l;
        printf("%d\n",ans);
    }
    return 0;
}
View Code

相关文章:

  • 2022-02-01
  • 2022-02-23
  • 2021-08-13
  • 2021-12-11
  • 2021-06-09
  • 2021-05-27
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2022-12-23
  • 2021-06-01
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案