QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#468472#1281. Longest Common SubsequencemaojunTL 0ms16260kbC++201.4kb2024-07-08 20:59:292024-07-08 20:59:31

Judging History

你现在查看的是最新测评结果

  • [2024-07-08 20:59:31]
  • 评测
  • 测评结果:TL
  • 用时:0ms
  • 内存:16260kb
  • [2024-07-08 20:59:29]
  • 提交

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,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,tr[S];
inline void bld(int n){memset(tr+1,0xc0,M<<3);for(M=1;M<=n;M<<=1);}
inline void upd(int p,int k){tr[p+=m+1+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+M;r+=m+M+2;
	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);
	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(m+m+1);
	for(int j=0;j<=c3;j++)upd(P[j]=Sa[Ra[j]-1]-Sb[Rb[j]-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]];
		ckmx(ans,qry(-m,Pi)+i-Sa[La[i]]);
		ckmx(ans,qry(Pi,m)+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: 16260kb

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...

output:


result: