This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 5050 , inf = 1e9 + 199;
int dp[N],ind[N];
int32_t main()
{
int n;
cin>>n;
assert(n <= 5000);
vector<int>a(n+1),k(n+1);
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
cin>>k[i];
}
dp[1]=1;
for(int i=2;i<=n;i++){
int maxi = 0 , indd = 1;
if(__builtin_popcount(a[i-1]) < k[i-1]){
continue;
}
for(int j=i-1;j>=1;j--){
if(__builtin_popcount((a[i-1]&a[j-1])) == k[i-1]){
if(dp[j]+1 > maxi) {
maxi = dp[j]+1;
indd = j;
}
}
}
if(maxi > 0){
dp[i] = maxi;
ind[i] = indd;
}
else{
dp[i]=1;
ind[i] = 0;
}
}
int maxi = 0 , indd = 1;
for(int i=1; i<=n; i++){
if(dp[i] >= maxi){
maxi = dp[i];
indd = i;
}
}
vector<int> res;
while(indd != 0){
res.push_back(indd);
indd = ind[indd];
}
cout<<res.size()<<endl;
sort(res.begin(), res.end());
for(int i:res){
cout<<i<<" ";
}
}
/*
*/
# | 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... |