# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1108844 | dubabuba | 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;
const int mxn = 1e5 + 10;
int a[mxn], dp[mxn];
int b[mxn], pr[mxn];
int n;
const int LOG = 8;
int mx[1 << LOG];
void solve() {
int id = 1;
for(int i = 1; i <= n; i++) {
dp[i] = 1;
for(int bit = 0; bit < (1 << LOG); bit++) {
int t = a[i] & bit;
if(__builtin_popcount(t) != b[i])
continue;
int j = mx[bit];
if(dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
pr[i] = j;
}
}
if(dp[id] < dp[i])
id = i;
if(dp[mx[a[i]]] < dp[i])
mx[a[i]] = i;
}
vector<int> ind;
while(id) {
ind.push_back(id);
id = pr[id];
}
reverse(ind.begin(), ind.end());
cout << ind.size() << endl;
for(int i : ind)
cout << i << ' ';
cout << endl;
exit(0);
}
int main() {
cin >> n;
for(int i = 1; i <= n; i++)
cin >> a[i];
for(int i = 1; i <= n; i++)
cin >> b[i];
if(i > 5000)
solve();
int id = 1;
for(int i = 1; i <= n; i++) {
dp[i] = 1;
for(int j = 1; j < i; j++) {
int t = a[i] & a[j];
if(__builtin_popcount(t) != b[i])
continue;
if(dp[j] + 1 > dp[i]) {
dp[i] = dp[j] + 1;
pr[i] = j;
}
}
if(dp[id] < dp[i])
id = i;
}
vector<int> ind;
while(id) {
ind.push_back(id);
id = pr[id];
}
reverse(ind.begin(), ind.end());
// if(ind.size()) return 1;
cout << (int)ind.size() << endl;
for(int i : ind)
cout << i << ' ';
cout << endl;
return 0;
}