QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#518256 | #1281. Longest Common Subsequence | cqzhm | TL | 3ms | 46684kb | C++14 | 1.6kb | 2024-08-13 18:52:51 | 2024-08-13 18:52:53 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN=1e6+10,N=1e6;
int n,m,a[MAXN],b[MAXN];
struct BIT{
int tree[MAXN<<1];
inline void clear(){memset(tree,0,sizeof tree);}
inline void init(){memset(tree,-0x3f,sizeof tree);}
inline int lowbit(int x){return x&(-x);}
inline void update(int x,int val){for(;x<MAXN*2;x+=lowbit(x)) tree[x]=max(tree[x],val);}
inline int query(int x){int ans=0; for(;x;x-=lowbit(x)) ans=max(ans,tree[x]); return ans;}
}f1,f2;
int la[MAXN],ra[MAXN],s1[MAXN];
int lb[MAXN],rb[MAXN],s2[MAXN];
int cnta1,cnta3,cntb1,cntb3;
inline void solve(){
cnta1=cnta3=cntb1=cntb3=0;
f1.clear(); f2.clear();
cin>>n>>m;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=m;i++) cin>>b[i];
for(int i=1;i<=n;i++){
if(a[i]==1) la[++cnta1]=i;
s1[i]=s1[i-1]+(a[i]==2);
}
ra[0]=n+1; for(int i=n;i>=1;i--) if(a[i]==3) ra[++cnta3]=i;
for(int i=1;i<=m;i++){
if(b[i]==1) lb[++cntb1]=i;
s2[i]=s2[i-1]+(b[i]==2);
}
rb[0]=m+1; for(int i=m;i>=1;i--) if(b[i]==3) rb[++cntb3]=i;
f1.init(); f2.init(); int ans=0;
for(int i=0,j=min(cnta3,cntb3);j>=0;j--){
while(i<=min(cnta1,cntb1) && ra[j]>la[i] && rb[j]>lb[i]){
f1.update(-s1[la[i]]+s2[lb[i]]+N,i-s1[la[i]]);
f2.update(s1[la[i]]-s2[lb[i]]+N,i-s2[lb[i]]);
i++;
}
int res1=f1.query(-s1[max(ra[j]-1,0ll)]+s2[max(rb[j]-1,0ll)]+N)+s1[max(ra[j]-1,0ll)]+j;
int res2=f2.query(s1[max(ra[j]-1,0ll)]-s2[max(rb[j]-1,0ll)]+N)+s2[max(rb[j]-1,0ll)]+j;
ans=max({ans,res1,res2});
}
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;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 46684kb
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
Time Limit Exceeded
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...