# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
885191 | alex_2008 | Longest beautiful sequence (IZhO17_subsequence) | C++14 | 70 ms | 6472 KiB |
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;
const int N = 1e5 + 10;
int a[N], k[N], dp[N];
int to[500];
int bitCount[(1 << 20)];
int way[N];
void precalc() {
bitCount[0] = 0;
for (int i = 1; i < (1 << 20); i++) {
bitCount[i] = bitCount[i / 2] + (i % 2);
}
}
int main() {
precalc();
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
for (int i = 0; i < n; i++) {
cin >> k[i];
}
if (n <= 5000) {
dp[0] = 1;
way[0] = -1;
for (int i = 1; i < n; i++) {
dp[i] = 1;
way[i] = -1;
for (int j = i - 1; j >= 0; j--) {
if (bitCount[(a[i] & a[j])] == k[i] && dp[i] < dp[j] + 1) {
dp[i] = dp[j] + 1;
way[i] = j;
}
}
}
int mxdp_ind = 0;
for (int i = 1; i < n; i++) {
if (dp[i] > dp[mxdp_ind]) {
mxdp_ind = i;
}
}
vector <int> ans;
int ind = mxdp_ind;
while (ind != -1) {
ans.push_back(ind + 1);
ind = way[ind];
}
cout << (int)ans.size() << "\n";
reverse(ans.begin(), ans.end());
for (int num : ans) {
cout << num << " ";
}
}
else {
for (int i = 0; i < 256; i++) {
to[i] = -1;
}
dp[0] = 1;
way[0] = -1;
to[a[0]] = 0;
for (int i = 1; i < n; i++) {
dp[i] = 1;
way[i] = -1;
for (int j = 0; j < 256; j++) {
if (to[j] == -1) continue;
if (bitCount[(j & a[i])] != k[i]) continue;
if (dp[to[j]] + 1 > dp[i]) {
dp[i] = dp[to[j]] + 1;
way[i] = to[j];
}
}
if (dp[i] > to[a[i]]) to[a[i]] = i;
}
int mxdp_ind = 0;
for (int i = 1; i < n; i++) {
if (dp[i] > dp[mxdp_ind]) {
mxdp_ind = i;
}
}
vector <int> ans;
int ind = mxdp_ind;
int cnt = 10;
while (ind != -1) {
ans.push_back(ind + 1);
ind = way[ind];
}
cout << (int)ans.size() << "\n";
reverse(ans.begin(), ans.end());
for (int num : ans) {
cout << num << " ";
}
}
}
Compilation message (stderr)
# | 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... |