QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#468061 | #4860. Longest Common Subsequence | Zi_Gao | TL | 0ms | 0kb | C++11 | 1.7kb | 2024-07-08 19:09:28 | 2024-07-08 19:09:28 |
answer
#include<bits/stdc++.h>
// #define ONLINE_JUDGE
#define INPUT_DATA_TYPE int
#define OUTPUT_DATA_TYPE int
inline __attribute((always_inline)) INPUT_DATA_TYPE read(){register INPUT_DATA_TYPE x=0;register char f=0,c=getchar();while(c<'0'||'9'<c)f=(c=='-'),c=getchar();while('0'<=c&&c<='9')x=(x<<3)+(x<<1)+(c&15),c=getchar();return f?-x:x;}void print(OUTPUT_DATA_TYPE x){if(x<0)x=-x,putchar('-');if(x>9)print(x/10);putchar(x%10^48);return;}
int suma[1000010],sumb[1000010],a[1000010],b[1000010];
void solve(){
std::vector<int> La,Ra,Lb,Rb;
register int i,j,res=0,maxi,maxj;
int n=read();
int m=read();
for(i=1;i<=n;++i){
a[i]=read();
if(a[i]==1) La.push_back(i);
else if(a[i]==3) Ra.push_back(i);
suma[i]=suma[i-1]+(a[i]==2);
}
for(i=1;i<=m;++i){
b[i]=read();
if(b[i]==1) Lb.push_back(i);
else if(b[i]==3) Rb.push_back(i);
sumb[i]=sumb[i-1]+(b[i]==2);
}
std::reverse(Ra.begin(),Ra.end());
std::reverse(Rb.begin(),Rb.end());
La.insert(La.begin(),0);
Ra.insert(Ra.begin(),n+1);
Lb.insert(Lb.begin(),0);
Rb.insert(Rb.begin(),n+1);
maxi=std::min({La.size(),Lb.size()})-1;
maxj=std::min({Ra.size(),Rb.size()})-1;
for(i=0;i<=maxi;++i)
for(j=0;j<=maxj;++j)
if(Ra[j]>La[i]&&Rb[j]>Lb[i]){
res=std::max(res,i+j+std::min(suma[Ra[j]-1]-suma[La[i]],sumb[Rb[j]-1]-sumb[Lb[i]]));
}
print(res),putchar('\n');
}
int main(){
#ifndef ONLINE_JUDGE
freopen("name.in", "r", stdin);
freopen("name.out", "w", stdout);
#endif
int T=read();
while(T--) solve();
#ifndef ONLINE_JUDGE
fclose(stdin);
fclose(stdout);
#endif
return 0;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
2 4 3 1024 1 1 1 1 3 4 1024 0 0 0 0