# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1006621 | Essa2006 | 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;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
int main() {
ios_base::sync_with_stdio(0);cin.tie(0);
int n;
cin >> n;
vector<int> A(n), K(n);
for (int i = 0; i < n; i++) {
cin >> A[i];
}
for (int i = 0; i < n; i++) {
cin >> K[i];
}
int ans = 0;
vector<array<int, 2>> dp(n);
for (int i = 0; i < n; i++) {
dp[i][0] = 1, dp[i][1] = -1;
for (int j = 0; j < i; j++) {
int res = (A[i] & A[j]);
if (pop_count(res) == K[i]) {
if (dp[j][0] + 1 > dp[i][0]) {
dp[i][0] = dp[j][0] + 1;
dp[i][1] = j;
}
}
}
if (dp[i][0] > dp[ans][0]) {
ans = i;
}
}
vector<int> Ans;
Ans.push_back(ans + 1);
while (dp[ans][1] != -1) {
ans = dp[ans][1];
Ans.push_back(ans + 1);
}
reverse(all(Ans));
cout << Ans.size() << endl;
for (int i = 0; i < Ans.size(); i++) {
cout << Ans[i] << ' ';
}
}