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 int ll
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<pi> vp;
typedef vector<pl> vpl;
const int maxn = 1e5 + 10;
vi t[maxn], cnt(256,0);
void init(vi a, vi k){
int n = k.size();
for (int i = 0; i < n; ++i){
int x = a[i];
for (int y = 0; y < 256; ++y){
if (__builtin_popcount((y&x)) == k[i])
t[i].push_back(y);
}
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);
int n;
cin >> n;
vi a(n);
for (auto& i : a)
cin >> i;
vi k(n);
for (auto& i : k)
cin >> i;
if (n <= 5000){
vi d(n), last(n, -1);
for (int i = 0; i < n; ++i){
d[i] = 1;
for (int j = 0; j < i; ++j){
if (d[j]+1 > d[i] && __builtin_popcount(a[i]&a[j]) == k[i]){
d[i] = d[j]+1;
last[i] = j;
}
}
}
int ind = max_element(d.begin(), d.end()) - d.begin();
cout << d[ind] << '\n';
vi sol;
while(ind != -1){
sol.push_back(ind+1);
ind = last[ind];
}
reverse(sol.begin(), sol.end());
for (auto& i : sol)
cout << i << ' ';
cout << '\n';
return 0;
}
init(a,k);
vi d(n), pos(256, -1), last(n, -1);
for (int i = 0; i < n; ++i){
d[i] = 1;
for (int x : t[i]){
if (pos[x] != -1 && d[i] < d[pos[x]]+1){
d[i] = d[pos[x]]+1;
last[i] = pos[x];
}
}
pos[a[i]] = i;
}
int ind = max_element(d.begin(), d.end())-d.begin();
vi sol;
while(ind != -1){
sol.push_back(ind);
ind = last[ind];
}
reverse(sol.begin(), sol.end());
cout << d[ind] << '\n';
for (auto& i : sol)
cout << i+1 << ' ';
}
# | 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... |