QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#499211 | #1281. Longest Common Subsequence | XiaoShanYunPan | WA | 110ms | 22400kb | C++14 | 1.9kb | 2024-07-31 08:48:50 | 2024-07-31 08:48:51 |
Judging History
answer
/*
为什么我总会认为一些简单的题难
首先注意到字符集只有1~3
所以可以枚举i,j,表示选i个1和j个3,然后算中间2的个数
这个东西很好维护,对于两个序列,分别设l[i],r[i]表示从左往右第i个1的位置和从右往左第i个3的位置
然后再设s[i]表示前i个位置有几个2,就可以维护答案
也就是i+j+min(sa[ra[j]]-sa[la[i]],sb[rb[j]]-sb[lb[i]])
但是直接做是O(n^2)的。我们考虑优化
尝试在枚举i时维护合法的j
不难发现,随着i逐渐增大,会使得一些j不可用
双指针维护当前可用的最小的j,然后查区间最小的j+r[j]即可
*/
#include<bits/stdc++.h>
using namespace std;
constexpr int N=1e6+10,INF=0x3f3f3f3f;
int t1[N],t2[N];
int lowbit(int x){return x&(-x);}
void add(int *tr,int x,int v){for(int i=x;i<N;i+=lowbit(i))tr[i]=min(tr[i],v);}
int sum(int *tr,int x){
if(x==0)return 0;
int res=INF;
for(int i=x;i;i-=lowbit(i))res=min(res,tr[i]);
return res;
}
int n,m,i,j,cnla,cnra,cnlb,cnrb,sa[N],sb[N],ans,a[N],b[N],la[N],lb[N],ra[N],rb[N];
void work(){
scanf("%d%d",&n,&m);
for(i=0;i<=n;++i)t1[i]=INF;
for(i=0;i<=m;++i)t2[i]=INF;
cnla=cnra=cnlb=cnrb=sa[0]=sb[0]=ans=0;
for(i=1;i<=n;++i)scanf("%d",a+i),sa[i]=sa[i-1]+(a[i]==2);
for(i=1;i<=m;++i)scanf("%d",b+i),sb[i]=sb[i-1]+(b[i]==2);
for(i=1;i<=n;++i)if(a[i]==1)la[++cnla]=i;
for(i=n;i;--i)if(a[i]==3)ra[++cnra]=i;ra[0]=n+1;
for(i=1;i<=m;++i)if(b[i]==1)lb[++cnlb]=i;
for(i=m;i;--i)if(b[i]==3)rb[++cnrb]=i;rb[0]=m+1;
for(i=1;i<=cnra;++i)add(t1,i,i+sa[ra[i]]);
for(i=1;i<=cnrb;++i)add(t2,i,i+sb[rb[i]]);
for(i=0,j=min(cnra,cnrb);i<=min(cnla,cnlb);++i){
while(la[i]>ra[j]||lb[i]>rb[j])--j;
int s=min(i+sum(t1,j)-sa[la[i]],i+sum(t2,j)-sb[lb[i]]);
ans=max(ans,s);
}
printf("%d\n",ans);
}
int T;
int main(){
scanf("%d",&T);
while(T--)work();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 22400kb
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: 110ms
memory: 20340kb
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:
0 0 0 2 1 1 0 1 2 1 1 1 1 4 0 0 1 1 0 3 1 1 0 1 1 0 2 0 2 3 4 1 1 2 2 0 2 1 2 0 0 1 1 0 1 1 2 0 1 2 0 1 1 1 2 1 3 0 1 2 2 0 0 3 4 2 1 0 0 0 2 4 3 2 1 1 2 3 1 0 1 0 4 0 2 2 1 2 2 2 5 0 0 3 0 3 0 1 0 2 1 3 1 2 0 1 0 2 2 2 0 0 0 0 1 3 1 2 0 1 0 4 1 1 2 0 0 0 0 1 0 1 3 2 1 2 1 1 3 0 1 2 1 1 1 1 2 2 2 1 ...
result:
wrong answer 1st words differ - expected: '1', found: '0'