QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468624 | #1281. Longest Common Subsequence | weiguoliang | RE | 0ms | 0kb | C++14 | 1.6kb | 2024-07-08 21:53:02 | 2024-07-08 21:53:03 |
answer
#include<bits/stdc++.h>
#define ll long long
#define N 1000005
#define lowbit(x) (x&-x)
using namespace std;
ll n,m,a[N],b[N],la[N],ra[N],suma[N],lb[N],rb[N],sumb[N],shu1[N<<1],shu2[N<<1],ans;
ll read(){char c=getchar();
ll n=0;
while(c<'0'||c>'9')c=getchar();
while(c<='9'&&c>='0')n=(n<<1)+(n<<3)+c-'0',c=getchar();
return n;
}
void add(ll x,ll w,ll shu[],ll n){
for(;x<=n;x+=lowbit(x))shu[x]=max(shu[x],w);
}
ll query(ll x,ll shu[]){ll ans=0;
for(;x;x-=lowbit(x))ans=max(ans,shu[x]);
return ans;
}
int main(){ll i,j,cnt1=0,cnt3=0,v,t;
t=read();
while(t--){memset(shu1,0,sizeof(shu1)),memset(shu2,0,sizeof(shu2)),cnt1=cnt3=0;
n=read(),m=read(),v=max(n,m);
for(i=1;i<=n;i++)a[i]=read();
for(i=1;i<=m;i++)b[i]=read();
for(i=1;i<=n;i++){cnt1+=(a[i]==1),cnt3+=(a[n-i+1]==3);
if(!la[cnt1]&&cnt1)la[cnt1]=i;
if(!ra[cnt3]&&cnt3)ra[cnt3]=n-i+1;
suma[i]=suma[i-1]+(a[i]==2);
}cnt1=cnt3=0;
for(i=1;i<=m;i++){cnt1+=(b[i]==1),cnt3+=(b[m-i+1]==3);
if(!lb[cnt1]&&cnt1)lb[cnt1]=i;
if(!rb[cnt3]&&cnt3)rb[cnt3]=m-i+1;
sumb[i]=sumb[i-1]+(b[i]==2);
}
for(i=1;la[i]!=0;i++)add(suma[la[i]]-sumb[lb[i]]+v,i-suma[la[i]],shu1,v*2),add(v-suma[la[i]]+sumb[lb[i]],i-sumb[lb[i]],shu2,v*2);
for(j=1,i=min(n,m);j<=min(n,m);j++){
while(ra[j]<=la[i]||rb[j]<=lb[i]||lb[i]==0||la[i]==0)i--;
ans=max(ans,max(query(suma[ra[j]-1]-sumb[rb[j]-1],shu1)+suma[ra[j]-1],query(v-suma[ra[j]-1]+sumb[rb[j]-1],shu2)+sumb[rb[j]-1])+j);
}
cout<<ans<<"\n";
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 3 3 1 2 3 1 2 3 3 3 1 1 1 1 1 2 3 3 1 3 2 1 2 3