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;
#define pb push_back
#define mp make_pair
typedef long long ll;
typedef pair<long long, long long> pll;
typedef pair<int,int> pii;
typedef vector<long long> vl;
typedef vector<int> vi;
int n;
vi arr, k;
int main(){
cin>>n;
arr.resize(n);
k.resize(n);
bool uslov = true;
for(int i = 0; i < n; i++)
{
cin>>arr[i];
if(arr[i] > (1 << 8)) uslov = false;
}
for(int i = 0; i < n; i++) cin>>k[i];
if(n <= 5000){
int dp[5001], from[5001], ans = 0;
memset(dp, 0, sizeof dp);
dp[0] = 1;
for(int i = 1; i < n; i++){
dp[i] = 1;
from[i] = i;
for(int j = i-1; j >= 0; j--){
if(__builtin_popcount(arr[i] & arr[j]) == k[i]){
dp[i] = max(dp[i], 1 + dp[j]);
if(dp[i] == 1 + dp[j]) from[i] = j;
}
}
}
vi ans_arr;
int at = 0;
for(int i = 0; i < n; i++){
if(ans < dp[i]){
ans = dp[i];
at = i;
}
}
while(from[at] != at){
ans_arr.pb(at);
at = from[at];
}
ans_arr.pb(at);
reverse(ans_arr.begin(), ans_arr.end());
cout<<ans<<endl;
for(auto t : ans_arr)
cout<<t+1<<" ";
cout<<endl;
}
else if(uslov){
int dp[100100], from[100100], pos[1<<8];
dp[0] = 1;
from[0] = 0;
memset(pos, -1, sizeof pos);
for(int i = 1; i < n; i++){
dp[i] = 1;
from[i] = i;
for(int j = 0; j < (1 << 8); j++){
if(__builtin_popcount(arr[i] & j) == k[i]){
if(pos[j] != -1){
dp[i] = max(dp[i], 1 + dp[pos[j]]);
if(dp[i] == 1 + dp[pos[j]])
from[i] = pos[j];
}
}
}
if(dp[i] > dp[pos[arr[i]]]){
pos[arr[i]] = i;
}
}
int ans = 0, at = 0;
for(int i = 0; i < n; i++)
{
ans = max(ans, dp[i]);
if(ans == dp[i])
at = i;
}
vi ans_arr;
while(from[at] != at){
ans_arr.pb(at);
at = from[at];
}
ans_arr.pb(at);
reverse(ans_arr.begin(), ans_arr.end());
cout<<ans<<endl;
for(auto t : ans_arr)
cout<<t+1<<" ";
cout<<endl;
}
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... |