CF2005E2题解
luckyclover · · 题解
我看不懂题解,但我会用 bitset!
考虑最简单的
直接把 dp 数组的第三维压进 bitset 里。在
时间复杂度
卡空间,需要滚动数组。
代码:
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
const int MAX=1505,MOD=998244353;
int n,m,K;
int a[MAX],b[MAX][MAX];
bitset<MAX> f[2][MAX];
unordered_map<int,bitset<MAX>> bs;
int main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T; cin>>T;
while(T--){
cin>>K>>n>>m;
for(int i=1;i<=m+1;i++){
f[0][i].reset(); f[1][i].reset();
}
bs.clear();
for(int i=1;i<=K;i++) cin>>a[i];
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
cin>>b[i][j];
for(int i=1;i<=K;i++) bs[a[i]][i]=1;
for(int i=n;i>=1;i--){
for(int j=m;j>=1;j--){
f[i&1][j]=f[i&1^1][j]|f[i&1][j+1];
if(bs.find(b[i][j])!=bs.end())
f[i&1][j]|=bs[b[i][j]]&(~(f[i&1^1][j+1]>>1));
}
}
if(f[1][1][1]) cout<<"T\n";
else cout<<"N\n";
}
return 0;
}