This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define f first
#define s second
#define pb push_back
#define ld long double
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define vec vector
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937_64 gen(chrono::steady_clock::now().time_since_epoch().count());
const ld eps = 1e-6;
const int mod = 998244353;
const int oo = 2e9;
const ll OO = 2e18;
const int N = 250001;
void solve()
{
int n;
cin >> n;
vec<int> a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
vec<int> k(n + 1);
for (int i = 1; i <= n; i++) cin >> k[i];
vec<int> dp(n + 1, 1);
vec<int> fr(n + 1, -1);
vec<int> last(256, -1);
for (int i = 1; i <= n; i++)
{
if (n <= 5000)
{
for (int j = 1; j < i; j++)
{
int x = a[j] & a[i];
if (__builtin_popcount(x) == k[i] && dp[j] + 1 > dp[i])
{
dp[i] = dp[j] + 1;
fr[i] = j;
}
}
}
else
{
for (int y = 0; y < 256; y++)
{
if (last[y] == -1) continue;
int x = y & a[i];
if (__builtin_popcount(x) == k[i] && dp[last[y]] + 1 > dp[i])
{
dp[i] = dp[last[y]] + 1;
fr[i] = last[y];
}
}
if (last[a[i]] == -1 || dp[i] > dp[last[a[i]]]) last[a[i]] = i;
}
}
vec<int> ans;
int best = 1;
for (int i = 2; i <= n; i++) if (dp[i] > dp[best]) best = i;
while (best != -1)
{
ans.pb(best);
best = fr[best];
}
reverse(all(ans));
cout << sz(ans) << "\n";
for (int &x : ans) cout << x << " ";
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
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... |