#include <bits/stdc++.h>
using i64 = long long;
#ifdef DEBUG
#include "debug.h"
#else
#define debug(...) void(23)
#endif
constexpr int MAX = 1024;
template<typename T>
bool chmax(T& lhs, T rhs) {
if (lhs < rhs) {
lhs = rhs;
return true;
}
return false;
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
int N;
std::cin >> N;
std::vector<int> A(N), K(N);
for (int i = 0; i < N; ++i) {
std::cin >> A[i];
}
for (int i = 0; i < N; ++i) {
std::cin >> K[i];
}
#ifdef DEBUG
for (int i = 0; i < N; ++i) {
for (int j = i + 1; j < N; ++j) {
if (__builtin_popcount(A[i] & A[j]) == K[j]) {
debug(i, j);
debug(i, std::bitset<20>(A[i]).to_string());
debug(j, std::bitset<20>(A[j]).to_string());
int il = A[i] >> 10;
int ir = A[i] & 1023;
int jl = A[j] >> 10;
int jr = A[j] & 1023;
debug(i, il);
debug(i, ir);
debug(j, jl);
debug(j, jr);
debug(il & jl, __builtin_popcount(il & jl));
debug(ir & jr, __builtin_popcount(ir & jr));
}
}
}
#endif
std::vector<std::pair<int, int>> dp(N, {1, -1});
std::vector bst(MAX, std::vector(MAX, std::vector<std::pair<int, int>>(11, {1, -1})));
std::vector popcnt(MAX, std::vector<int>(MAX));
for (int i = 0; i < MAX; ++i) {
for (int j = 0; j < MAX; ++j) {
popcnt[i][j] = __builtin_popcount(i & j);
}
}
for (int i = 0; i < N; ++i) {
int l = A[i] >> 10;
int r = A[i] & 1023;
debug(i, l, r);
for (int j = 0; j < MAX; ++j) {
int need = K[i] - popcnt[l][j];
if (need < 0 || need > 10) {
continue;
}
chmax(dp[i], bst[j][r][need]);
}
debug(i, dp[i]);
for (int j = 0; j < MAX; ++j) {
int used = popcnt[j][r];
chmax(bst[l][j][used], std::pair{dp[i].first + 1, i});
}
}
auto it = std::max_element(dp.begin(), dp.end());
int ans = it->first;
int p = it - dp.begin();
std::vector<int> vec;
for (int i = 0; i < ans; ++i) {
vec.emplace_back(p);
p = dp[p].second;
}
std::reverse(vec.begin(), vec.end());
std::cout << ans << '\n';
assert(ans == vec.size());
for (auto i : vec) {
std::cout << i + 1 << " \n"[i == vec.back()];
}
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... |