Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
37526 LYLAKIOI 【BJ】T2 C++ 运行超时 60 2000 MS 6764 KB 2681 2025-04-04 19:53:07

Tests(31/32):


#include<bits/stdc++.h> #define up(i,l,r) for(int i=(l);i<=(r);++i) #define down(i,l,r) for(int i=(l);i>=(r);--i) #define pi pair<int,int> #define p1 first #define p2 second #define m_p make_pair #define p_b push_back #define ppc __builtin_popcountll using namespace std; typedef long long ll; typedef unsigned long long ull; typedef unsigned int uint; const int maxn=1e6+10,mod=998244353; inline ll read(){ ll x=0;short t=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')t=-1;ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar(); return x*t; } int n,m,a[maxn],sa[maxn],sb[maxn]; int f[3],g[3],tf[3],tg[3]; bool L,R; int iv[maxn],pre[maxn],nxt[maxn]; int calc(int l,int r){ L=0,R=0; if(pre[l])f[0]=sa[pre[l]-1],g[0]=sb[pre[l]-1];else f[0]=0,g[0]=0,L=1; if(nxt[r]<=n+m)f[2]=sa[n+m]-sa[nxt[r]],g[2]=sb[n+m]-sb[nxt[r]];else f[2]=0,g[2]=0,R=1; f[1]=sa[nxt[r]-1]-sa[pre[l]],g[1]=sb[nxt[r]-1]-sb[pre[l]]; up(i,0,2)tf[i]=f[i],tg[i]=g[i];bool tL=L,tR=R; int st=0,ans=0; while((!L)||(!R)){ ++ans; if(!st)R|=!f[2],f[2]--; else L|=!f[0],f[0]--;st^=1; if(!L){ if(g[1])--g[1],++f[1]; else if(g[2])--g[2],++f[2]; else break; } else { if(g[1])--g[1],++f[1]; else if(g[0])--g[0],++f[0]; else break; } } if(L&&R);else ans=1e9; up(i,0,2)f[i]=tf[i],g[i]=tg[i];L=tL,R=tR; st=0; int tot=0; while((!L)||(!R)){ ++tot; if(!st)R|=!f[2],f[2]--; else L|=!f[0],f[0]--;st^=1; if(R){ if(g[1])--g[1],++f[1]; else if(g[2])--g[2],++f[2]; else break; } else { if(g[1])--g[1],++f[1]; else if(g[0])--g[0],++f[0]; else break; } } if(L&&R)ans=min(ans,tot); return ans; } void slv(){ n=read(),m=read(); up(i,1,n)a[i]=read(),iv[a[i]]=i,++sa[a[i]]; up(i,1,m)++sb[read()]; up(i,1,n+m)sa[i]+=sa[i-1],sb[i]+=sb[i-1]; int R=0,res=1e9; vector<int>P={0};up(i,1,n+m)if(iv[i])P.p_b(i); int pos=0;up(i,1,n+m)if(iv[i])pre[i]=pos,pos=i; pos=n+m+1;down(i,n+m,1)if(iv[i])nxt[i]=pos,pos=i; up(i,1,n){ R=max(R,i); while(R+1<=n&&iv[P[R]]<=iv[P[R+1]])R++; res=min(res,calc(P[i],P[R])); } if(res<=n+m)printf("%d\n",res); else printf("-1"); } int main(){ //freopen("1.in","r",stdin),freopen("1.out","w",stdout); //int t=read();while(t--)slv(); slv(); //cerr<<clock()*1.0/CLOCKS_PER_SEC<<"s\n"; return 0; }


测评信息: