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>
using namespace std;
using ll = long long;
#define pb push_back
#define dbg(x) cerr << #x << " " << x << "\n"
const int MAX_N = 1e5, LG = 20, HALF = 10;
int dp[1 + MAX_N];
int from[1 + MAX_N];
int best_pos[1 << HALF][1 << HALF][1 + HALF];
int a[1 + MAX_N], k[1 + MAX_N];
int main () {
ios::sync_with_stdio (false);
cin.tie (0); cout.tie (0);
int n;
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
for (int i = 1; i <= n; i++)
cin >> k[i];
for (int i = 1; i <= n; i++) {
int mask_right = (a[i] & ((1 << HALF) - 1));
int mask_left = (a[i] >> HALF);
int best_index = 0;
for (int mask = 0; mask < (1 << HALF); mask++) {
int nr_bits = k[i] - __builtin_popcount (mask_right & mask);
if (nr_bits >= 0 && nr_bits <= HALF && dp[best_index] < dp[best_pos[mask_left][mask][nr_bits]])
best_index = best_pos[mask_left][mask][nr_bits];
}
dp[i] = dp[best_index] + 1;
from[i] = best_index;
for (int mask = 0; mask < (1 << HALF); mask++) {
int nr_bits = __builtin_popcount (mask_left & mask);
if (dp[i] > dp[best_pos[mask][mask_right][nr_bits]])
best_pos[mask][mask_right][nr_bits] = i;
}
}
int mx = 0;
for (int i = 1; i <= n; i++)
if (dp[i] > dp[mx])
mx = i;
int cur = mx;
vector <int> sol;
while (cur) {
// dbg (cur);
sol.pb (cur);
cur = from[cur];
}
reverse (sol.begin (), sol.end ());
cout << sol.size () << "\n";
for (int x : sol)
cout << x << " ";
cout << "\n";
return 0;
}
/**
4
1 2 3 4
10 0 1 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... |