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 && !uslov){
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){
vector<int> pos[(1<<8)];
for(int i = 0; i < n; i++){
pos[arr[i]].pb(i);
}
int dp[100100], from[100100];
dp[0] = 1;
from[0] = 0;
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]){
for(auto t : pos[j]){
dp[i] = max(dp[i], 1 + dp[t]);
if(dp[i] == 1 + dp[t]){
from[i] = t;
}
}
}
}
}
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... |