#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;
//#ifndef DLOCAL
//#define cin fin
//#define cout fout
//ifstream cin("subsequence.in");
//ofstream cout("subsequence.out");
//#endif
using ll = long long;
using ld = long double;
//#define int ll
#define sz(x) ((int)(x).size())
using pii = pair<int,int>;
using tii = tuple<int,int,int>;
#define real samiosugitralala
pii dp[1 << 20][20];
int ppc[1 << 20];
const int nmax = 1e5 + 5;
int lst[nmax], sum[nmax];
int v[nmax], k[nmax];
signed main() {
for(int i = 0; i < (1 << 20); i++) ppc[i] = ppc[i >> 1] + (i & 1);
cin.tie(0) -> sync_with_stdio(0);
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> v[i];
for(int j = 1; j <= n; j++) cin >> k[j];
reverse(v + 1, v + n + 1);
reverse(k + 1, k + n + 1);
const int B = n / 5 + 1;
pii globalbest(0, 0);
for(int i = 1; i <= n; i++) {
pii best = dp[v[i]][0];
for(int j = i - 1; j % B != 0; j--)
if(ppc[v[j] & v[i]] == k[j]) best = max(best, make_pair(sum[j], j));
tie(sum[i], lst[i]) = best;
//cerr << v[i] << ' ' << v[lst[i]] << ' ' << k[lst[i]] << '\t' << i << ' ' << lst[i] << '\n';
sum[i]++;
globalbest = max(globalbest, make_pair(sum[i], i));
if(i % B == 0) {
//cerr << "wah\n";
for(int step = 0; step < 20; step++)
for(int msk = 0; msk < (1 << 20); msk++)
dp[msk][step] = pii{0, 0};
for(int j = 1; j <= i; j++) {
dp[v[j]][k[j]] = max(dp[v[j]][k[j]], make_pair(sum[j], j));
}
pii a, b;
for(int step = 0; step < 20; step++) {
for(int msk = 0; msk < (1 << 20); msk++) {
if(msk & (1 << step)) continue;
for(int h = 0; h <= ppc[msk >> step]; h++) {
a = dp[msk][h], b = dp[msk | (1 << step)][h];
dp[msk][h] = max(a, b);
dp[msk | (1 << step)][h] = max(a, dp[msk | (1 << step)][h + 1]);
}
}
}
}
}
cout << globalbest.first << '\n';
vector<int> vo;
while(globalbest.second != 0) {
vo.emplace_back(globalbest.second);
globalbest.second = lst[globalbest.second];
}
for(auto x : vo) cout << n + 1 - x << ' ';
cout << '\n';
reverse(v + 1, v + n + 1);
reverse(k + 1, k + n + 1);
for(int i = 1; i < sz(vo); i++) {
if(ppc[v[vo[i]] & v[vo[i - 1]]] != k[vo[i]]) { cerr << "muie\n"; assert(false); }
}
}
/**
Binecuvanteaza Doamne Ukraina.
--
*/
# | 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... |