#include <bits/stdc++.h>
using namespace std;
ifstream fin("subsequence.in");
ofstream fout("subsequence.out");
const int Nmax=1e5+5;
int a[Nmax],k[Nmax];
int dp[Nmax],prv[Nmax];
int best[260];
void reconst(int x)
{
if(x==-1) return;
reconst(prv[x]);
cout<<x<<' ';
return;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
for(int i=1;i<=n;i++) cin>>k[i];
if(n<=5000)
{
int sol=0,besti=-1;
for(int i=1;i<=n;i++)
{
dp[i]=1;
prv[i]=-1;
for(int j=i-1;j>=1;j--)
{
int nr=(a[i]&a[j]);
nr=__builtin_popcount(nr);
if(nr==k[i] && dp[j]+1>dp[i])
{
dp[i]=dp[j]+1;
prv[i]=j;
}
}
if(dp[i]>sol)
{
sol=dp[i];
besti=i;
}
}
cout<<sol<<'\n';
reconst(besti);
}
else
{
int sol=0,besti=-1;
for(int i=1;i<=n;i++)
{
dp[i]=1;
prv[i]=-1;
for(int j=0;j<=256;j++)
{
if(best[j]==0) continue;
int nr=(a[i]&j);
nr=__builtin_popcount(nr);
if(nr==k[i] && dp[best[j]]+1>dp[i])
{
dp[i]=dp[best[j]]+1;
prv[i]=best[j];
}
}
if(dp[i]>dp[best[a[i]]]) best[a[i]]=i;
if(dp[i]>sol)
{
sol=dp[i];
besti=i;
}
}
cout<<sol<<'\n';
reconst(besti);
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |