# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
939743 | ifateen | Longest beautiful sequence (IZhO17_subsequence) | C++14 | 0 ms | 0 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;
bool ckmax(int& a, int b) {
if (a < b) return a = b, true;
return false;
}
const int MAXN = 2001;
int dp[MAXN], prv[MAXN], a[MAXN], k[MAXN];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
dp[i] = 1;
prv[i] = i;
}
for (int i = 0; i < n; i++) {
cin >> k[i];
}
for (int i = 1; i < n; i++)
for (int j = 0; j < i; j++)
if (__builtin_popcount(a[i] & a[j]) == k[i] && ckmax(dp[i], 1 + dp[j]))
prv[i] = j;
int mx = max_element(dp, dp + n) - dp;
vector<int> ans;
ans.push_back(mx);
while (mx != prv[mx]) {
mx = prv[mx];
ans.push_back(mx);
}
reverse(begin(ans), end(ans));
cout << size(ans) << '\n';
for (auto& i : ans) cout << i + 1 << ' ';
}