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"
// @JASPER'S BOILERPLATE
using namespace std;
using ll = long long;
#ifdef JASPER
#include "debug.h"
#else
#define debug(...) 166
#endif
const int M = 9;
signed main() {
cin.tie(0) -> sync_with_stdio(0);
int n; cin >> n;
vector <int> a(n + 5), k(n + 5);
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> k[i];
// subtask 1-2:
vector <int> opt(n + 5, -1);
int p = 0, ans = 0;
if (n <= 5000) {
vector <int> dp(n + 5, 1);
for (int i = 1; i <= n; ++i) {
for (int j = 1; j < i; ++j) {
int cur = (__builtin_popcount(a[i] & a[j]) == k[i])? dp[j] : 0;
if (dp[i] < cur + 1) {
opt[i] = j;
dp[i] = cur + 1;
}
}
if (ans < dp[i]) {
p = i;
ans = dp[i];
}
}
}
else {
vector <pair <int, int>> dp(1 << M), nxt_dp(1 << M);
vector <int> f(n + 5, 1);
for (int i = 1; i <= n; ++i) {
nxt_dp[a[i]].first = max(nxt_dp[a[i]].first, 1);
for (int msk = 0; msk < (1 << M); ++msk) {
int cnt = __builtin_popcount(a[i] & msk);
if (cnt == k[i] && nxt_dp[a[i]].first < dp[msk].first + 1) {
nxt_dp[a[i]].first = dp[msk].first + 1;
opt[i] = dp[msk].second;
}
}
for (int msk = 0; msk < (1 << M); ++msk) {
if (dp[msk].first != nxt_dp[msk].first)
nxt_dp[msk].second = i;
f[i] = max(f[i], nxt_dp[msk].first);
}
dp = nxt_dp;
// debug(dp);
}
for (int i = 1; i <= n; ++i) {
if (ans < f[i]) {
ans = f[i];
p = i;
}
}
}
// debug(opt);
vector <int> ids;
while (p != -1) {
ids.push_back(p);
p = opt[p];
}
reverse(ids.begin(), ids.end());
cout << ans << "\n";
for (int x : ids) cout << x << " ";
cout << "\n";
}
# | 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... |