QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468500 | #1281. Longest Common Subsequence | hyxle | WA | 83ms | 18072kb | C++14 | 1.9kb | 2024-07-08 21:09:55 | 2024-07-08 21:09:55 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define R register
using namespace std;
const int N=1e6+5;
int n,m,a[N],b[N],la[N],ra[N],suma[N],sumb[N],lb[N],rb[N],tota,totb,cura,curb,mx,cnt1,cnt3;
struct BIT{
int nd[N<<1];
#define lowbit(x) (x&-x)
inline void clear(){for(R int i=1;i<=mx;++i)nd[i]=0;}
inline void update(int x,int k){
while(x<=mx){nd[x]=max(nd[x],k);x+=lowbit(x);}
}
inline int query(int x){int res=0;while(x){res=max(res,nd[x]);x^=lowbit(x);}return res;}
}tr[2];
inline void solve(){
cin>>n>>m;tota=totb=cura=curb=0;
for(R int i=1;i<=n;++i)cin>>a[i],la[i]=ra[i]=0;
for(R int i=1;i<=m;++i)cin>>b[i],lb[i]=rb[i]=0;
for(R int i=1;i<=n;++i)if(a[i]==1)la[++tota]=i;
for(R int i=1;i<=m;++i)if(b[i]==1)lb[++totb]=i;
for(R int i=n;i>=1;--i)if(a[i]==3)ra[++cura]=i;
for(R int i=m;i>=1;--i)if(b[i]==3)rb[++curb]=i;
for(R int i=1;i<=n;++i)suma[i]=suma[i-1]+(a[i]==2);
for(R int i=1;i<=m;++i)sumb[i]=sumb[i-1]+(b[i]==2);
cnt1=min(tota,totb);cnt3=min(cura,curb);mx=2*max(n,m)+1;
tr[0].clear(),tr[1].clear();int maxx=0;
ra[0]=n+1,rb[0]=m+1,suma[n+1]=suma[n],sumb[m+1]=sumb[m];
for(R int j=cnt3,i=-1;j>=0;--j){
while(i+1<=cnt1&&ra[j]>la[1+i]&&rb[j]>lb[i+1]){
++i;
int pos=suma[la[i]]-sumb[lb[i]];
tr[0].update(n+1-pos,i-suma[la[i]]);
tr[1].update(m+1+pos,i-sumb[lb[i]]);
}
int pos=suma[ra[j]-1]-sumb[rb[j]-1];
maxx=max(maxx,j+suma[ra[j]-1]+tr[0].query(n+1-pos));maxx=max(maxx,j+sumb[rb[j]-1]+tr[1].query(m+1+pos));
}
cout<<maxx<<'\n';
}
int main(){
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
int _;cin>>_;while(_--)solve();
return 0;
}
/*
max{i+j+min(suma[ra[j]-1]-suma[la[i]],sumb[rb[j]-1]-sumb[lb[i]])}
=>
suma[ra[j]-1]-sumb[rb[j]-1]<=sumb[la[i]]-suma[lb[i]]
或
sumb[la[i]]-suma[lb[i]]<=suma[ra[j]-1]-sumb[rb[j]-1]
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 18072kb
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
output:
3 2 2
result:
ok 3 tokens
Test #2:
score: -100
Wrong Answer
time: 83ms
memory: 18056kb
input:
139889 1 10 2 2 1 2 2 3 1 1 2 3 3 7 1 3 2 3 3 1 1 2 2 6 1 2 1 3 1 1 1 1 8 7 3 1 3 3 2 2 3 1 3 2 2 1 3 3 3 10 3 3 2 1 1 2 2 1 1 1 1 3 1 1 5 2 1 2 1 3 1 1 2 1 4 1 3 3 3 3 7 2 3 1 2 1 2 2 3 3 2 6 2 3 1 1 2 1 3 1 3 1 4 1 3 1 1 3 4 2 2 3 2 3 1 3 1 8 1 1 1 3 1 3 3 3 1 4 1 1 3 3 3 1 10 10 3 1 2 1 2 2 2 2 1...
output:
4 2 1 5 3 2 0 4 2 1 3 1 1 8 2 2 1 3 2 4 5 4 1 3 2 1 3 2 3 4 4 2 3 4 4 5 5 2 3 4 1 1 3 1 1 5 5 4 1 4 2 4 1 4 2 1 4 1 3 4 4 2 3 4 5 2 3 3 2 2 4 4 3 2 2 3 2 4 4 3 2 2 5 2 6 4 1 3 3 4 5 4 1 4 1 6 2 4 1 4 6 4 3 2 1 2 1 5 2 4 3 5 1 5 2 3 2 3 0 4 2 4 1 3 4 5 1 1 1 4 2 2 4 3 2 4 3 5 3 1 5 6 6 2 3 4 3 4 3 3 ...
result:
wrong answer 1st words differ - expected: '1', found: '4'