QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#517551 | #1281. Longest Common Subsequence | wy2025 | WA | 84ms | 20052kb | C++14 | 1.7kb | 2024-08-13 12:10:04 | 2024-08-13 12:10:04 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=1e6+10,inf=0x3f3f3f3f;
int n,m,a[2][N],L[2][N],R[2][N],s[2][N],cl[2],cr[2];//0a,1b
struct T_A{
int c[N*4];
void init(){for(int i=1;i<=2*(n+m+1);i++) c[i]=-inf;}
int lowbit(int x){return x&-x;}
void update(int x,int v){x+=n+m+1;for(int i=x;i;i-=lowbit(i)) c[i]=max(c[i],v);}
int ask(int x){
x+=n+m+1;
int ans=-inf;
for(int i=x;i<=2*(n+m+1);i+=lowbit(i)) ans=max(ans,c[i]);
return ans;
}
}t[2];
void solve(){
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[0][i];
for(int i=1;i<=m;i++) cin>>a[1][i];
cl[0]=cr[0]=cl[1]=cr[1]=0;
R[0][0]=n+1;
t[0].init();
for(int i=1;i<=n+1;i++){
if(a[0][i]==1) L[0][++cl[0]]=i;
s[0][i]=s[0][i-1]+(a[0][i]==2);
}
for(int i=n;i;i--)
if(a[0][i]==3) R[0][++cr[0]]=i;
R[1][0]=m+1;
t[1].init();
for(int i=1;i<=m+1;i++){
if(a[1][i]==1) L[1][++cl[1]]=i;
s[1][i]=s[1][i-1]+(a[1][i]==2);
}
for(int i=m;i;i--)
if(a[1][i]==3) R[1][++cr[1]]=i;
int ans=min(s[0][n],s[1][m]);
for(int i=0,j=min(cr[0],cr[1]);~j;j--){
while(i<=min(cl[0],cl[1])&&L[0][i]<R[0][j]&&L[1][i]<R[1][j])
t[0].update(s[0][L[0][i]]-s[1][L[1][i]],i-s[0][L[0][i]]),
t[1].update(s[1][L[1][i]]-s[0][L[0][i]],i-s[1][L[1][i]]),i++;
ans=max(ans,j+max(s[0][R[0][j]]+t[0].ask(s[0][R[0][j]]-s[1][R[1][j]]),
s[1][R[1][j]]+t[1].ask(s[1][R[1][j]]-s[0][R[0][j]])));
// cout<<ans<<" "<<j<<" "<<s[0][R[0][j]]<<" "<<t[1].ask(s[1][R[1][j]]-s[0][R[0][j]])<<"\n";
}
cout<<ans<<"\n";
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int t;
cin>>t;
while(t--) solve();
return 0;
}
/*
1
8 7
3 1 3 3 2 2 3 1
3 2 2 1 3 3 3
1
1 6
1
1 1 2 2 3 3
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 20052kb
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: 84ms
memory: 17928kb
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 1 1 4 2 2 0 1 2 1 1 1 1 6 2 1 2 2 2 3 4 4 1 2 1 0 2 2 3 4 4 1 1 3 2 1 2 2 4 2 2 1 3 1 1 2 3 2 2 3 1 4 2 2 2 1 3 1 1 2 3 1 2 4 4 2 2 1 2 1 3 4 3 2 1 1 2 3 1 3 3 1 5 3 4 2 1 3 4 2 5 3 1 3 1 3 1 2 1 2 2 4 3 2 1 1 1 3 2 4 2 3 1 0 3 3 1 2 0 4 1 4 1 1 3 1 1 1 1 2 2 1 3 2 2 2 3 3 3 1 2 2 4 2 1 2 3 3 2 3 ...
result:
wrong answer 15th words differ - expected: '1', found: '2'