QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#468487 | #1281. Longest Common Subsequence | maojun | WA | 107ms | 16128kb | C++20 | 1.5kb | 2024-07-08 21:04:41 | 2024-07-08 21:04:42 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline void ckmx(int&x,int y){x<y&&(x=y);}
const int N=1e6+5,O=1e9;
int n,m,t,a[N],b[N];
int Sa[N],La[N],Ra[N],Sb[N],Lb[N],Rb[N],P[N];
inline void rd(int a[],int n,int S[],int L[],int R[]){
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
L[0]=0;R[0]=n+1;
for(int i=1,c=0;i<=n;i++)if(a[i]==1)L[++c]=i;
for(int i=n,c=0;i>=1;i--)if(a[i]==3)R[++c]=i;
for(int i=1;i<=n;i++)S[i]=S[i-1]+(a[i]==2);
}
const int S=N<<3;
int M=0,tr[S];
inline void bld(int n){memset(tr+1,0xc0,n+M<<2);for(M=1;M<=n;M<<=1);}
inline void upd(int p,int k){tr[p+=M]=k;while(p>>=1)tr[p]=max(tr[p<<1],tr[p<<1|1]);}
inline int qry(int l,int r){
int res=-O;l+=M-1;r+=M+1;
for(;l^r^1;l>>=1,r>>=1){
if(~l&1)ckmx(res,tr[l^1]);
if(r&1)ckmx(res,tr[r^1]);
}
return res;
}
inline void solve(){
scanf("%d%d",&n,&m);t=max(n,m);
rd(a,n,Sa,La,Ra);rd(b,m,Sb,Lb,Rb);
int c1a=0,c3a=0,c1b=0,c3b=0;
for(int i=1;i<=n;i++){c1a+=a[i]==1;c3a+=a[i]==3;}
for(int i=1;i<=m;i++){c1b+=b[i]==1;c3b+=b[i]==3;}
int c1=min(c1a,c1b),c3=min(c3a,c3b);
bld(t+t+1);
for(int j=0;j<=c3;j++)upd(P[j]=Sa[Ra[j]-1]-Sb[Rb[j]-1]+t+1,j+Sa[Ra[j]-1]);
int ans=0;
for(int i=0,j=c3;i<=c1;i++){
for(;~j&&(Ra[j]<La[i]||Rb[j]<Lb[i]);j--)upd(P[j],-O);
int res=-O,Pi=Sa[La[i]]-Sb[Lb[i]]+t+1;
ckmx(ans,qry(1,Pi)+i-Sa[La[i]]);
ckmx(ans,qry(Pi,t+t+1)+i-Sb[Lb[i]]);
}
printf("%d\n",ans);
}
int main(){
int T;scanf("%d",&T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 16128kb
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: 107ms
memory: 16076kb
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:
1 2 2 4 5 2 0 4 3 1 3 1 1 6 1 0 2 3 2 3 4 5 2 3 3 1 2 2 5 4 3 3 3 3 4 6 2 1 4 4 2 1 3 1 1 6 5 4 1 5 1 5 1 4 2 2 3 1 3 5 4 2 3 6 5 3 2 1 3 2 2 5 3 2 2 3 2 4 4 2 2 2 7 2 4 2 1 3 3 2 5 4 1 5 2 3 3 2 1 2 3 4 3 2 0 2 1 6 2 2 3 5 0 0 3 3 2 3 0 6 1 3 1 3 5 6 1 0 1 1 2 1 3 3 2 4 3 7 3 1 2 2 3 2 1 1 3 4 2 3 ...
result:
wrong answer 2nd words differ - expected: '1', found: '2'