QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#470715 | #1281. Longest Common Subsequence | zhouyuxuan3501 | TL | 0ms | 28452kb | C++14 | 1.6kb | 2024-07-10 16:01:52 | 2024-07-10 16:01:52 |
Judging History
answer
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int n_max=1e6+11;
struct BIT
{
int t[n_max<<1],N=n_max<<1;
void Z(int x,int c)
{
for(;x<N;x+=(-x&x))
t[x]=max(t[x],c);
}
int C(int x)
{
int c=-2e9;
for(;x;x-=(-x&x))
c=max(c,t[x]);
return c;
}
void Q()
{
memset(t,0,sizeof t);
}
}T1,T2;
int n,m,A[n_max],B[n_max],c1[2][n_max],c2[2][n_max],c3[2][n_max],k1[2],k3[2],i,j,h,w,y,t;
void CN()
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&A[i]);
if(A[i]==1)
c1[0][++k1[0]]=i;
c2[0][i]=c2[0][i-1]+(A[i]==2);
}
for(i=n;i>=1;i--)
if(A[i]==3)
c3[0][++k3[0]]=i;
c2[0][n+1]=c2[0][n];
c3[0][0]=n+1;
for(i=1;i<=m;i++)
{
scanf("%d",&B[i]);
if(B[i]==1)
c1[1][++k1[1]]=i;
c2[1][i]=c2[1][i-1]+(B[i]==2);
}
for(i=m;i>=1;i--)
if(B[i]==3)
c3[1][++k3[1]]=i;
c2[1][m+1]=c2[1][m];
c3[1][0]=m+1;
h=min(k1[0],k1[1]);
w=min(k3[0],k3[1]);
for(i=h;i>=0;i--)
{
while(j<=w&&c1[0][j]<c3[0][j]&&c1[1][j]<c3[1][j])
{
T1.Z(c2[0][c3[0][j]]-c2[1][c3[1][j]]+m+1,j+c2[0][c3[0][j]]);//m+1避免下标为负数
y=max(y,T1.C(m+1+c2[0][c1[0][i]]-c2[1][c1[1][i]])+i-c2[0][c1[0][i]]);
j++;
}
}
j=0;
for(i=h;i>=0;i--)
{
while(j<=w&&c1[0][j]<c3[0][j]&&c1[1][j]<c3[1][j])
{
T2.Z(c2[1][c3[1][j]]-c2[0][c3[0][j]]+n+1,j+c2[1][c3[1][j]]);
y=max(y,T2.C(n+1+c2[1][c1[1][i]]-c2[0][c1[0][i]])+i-c2[1][c1[1][i]]);
j++;
}
}
printf("%d\n",y);
}
int main()
{
scanf("%d",&t);
while(t--)
{
k1[0]=k1[1]=k3[0]=k3[1]=0;
y=0;
T1.Q();
T2.Q();
CN();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 28452kb
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:
1 1 1 2 2 2 0 1 2 1 1 1 1 4 1 0 1 1 2 0 1 4 1 1 0 0 2 1 3 4 2 1 0 2 2 1 0 1 3 1 1 0 1 1 0 2 2 1 1 3 0 2 1 1 2 1 3 1 1 2 2 1 2 4 4 2 2 0 1 1 3 4 2 1 1 1 2 2 0 2 1 1 5 2 2 0 1 3 3 2 5 2 1 3 1 3 1 0 1 3 0 3 1 2 0 0 1 2 2 4 2 2 0 0 2 3 1 1 0 4 0 4 0 0 2 1 1 0 1 1 1 1 3 1 1 2 1 3 3 1 1 2 3 2 0 0 1 2 2 3 ...