#include <bits/stdc++.h>
using namespace std;
#define pii pair<int,int>
#define fi first
#define se second
const int N = 1e5 + 5;
int a[N],k[N],n;
namespace sub2 {
int dp[N],trace[N];
void solve() {
int ans = 0,cur = 0;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int j = 1; j < i; j++)
if (__builtin_popcount(a[i] & a[j]) == k[i] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
trace[i] = j;
}
if (dp[i] > ans) ans = dp[i],cur = i;
}
vector<int>vec;
for (int i = cur; i > 0; i = trace[i]) vec.push_back(i);
reverse(vec.begin(),vec.end());
cout << ans << "\n";
for (auto x: vec) cout << x << " ";
cout << "\n";
}
}
namespace sub3 {
int dp[N],trace[N];
pii best[260];
bool check_condition() {
for (int i = 1; i <= n; i++)
if (a[i] >= (1 << 8)) return false;
return true;
}
void solve() {
int ans = 0,cur = 0;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
for (int mask = 0; mask < (1 << 8); mask++) {
if (__builtin_popcount(mask & a[i]) == k[i] && best[mask].fi + 1 > dp[i]) {
dp[i] = best[mask].fi + 1;
trace[i] = best[mask].se;
}
}
if (dp[i] > best[a[i]].fi) best[a[i]] = {dp[i],i};
if (dp[i] > ans) ans = dp[i],cur = i;
}
vector<int>vec;
for (int i = cur; i > 0; i = trace[i]) vec.push_back(i);
reverse(vec.begin(),vec.end());
cout << ans << "\n";
for (auto x: vec) cout << x << " ";
cout << "\n";
}
}
namespace sub4 {
int dp[N],trace[N];
pii best[(1 << 10) + 5][15];
void solve() {
int MASK = (1 << 10),ans = 0,cur = 0;
for (int i = 1; i <= n; i++) {
dp[i] = 1;
int l = (a[i] >> 10),r = a[i] %= MASK; //chia thanh 2 phan, moi phan 10 bit
for (int mask = 0; mask < MASK; mask++) {
int num = k[i] - __builtin_popcount(l & mask);
if (num < 0 || num > 10) continue;
if (best[r][num].fi + 1 > dp[i]) {
dp[i] = best[r][num].fi + 1;
trace[i] = best[r][num].se;
}
}
if (dp[i] > ans) ans = dp[i],cur = i;
for (int mask = 0; mask < MASK; mask++) {
int num = __builtin_popcount(r & mask);
if (dp[i] > best[mask][num].fi) best[mask][num] = {dp[i],i};
}
}
vector<int>vec;
for (int i = cur; i > 0; i = trace[i]) vec.push_back(i);
reverse(vec.begin(),vec.end());
cout << ans << "\n";
for (auto x: vec) cout << x << " ";
cout << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> k[i];
//if (n <= 5000) sub2::solve();
//else if (sub3::check_condition()) sub3::solve();
//else
sub4::solve();
}
# | 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... |