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>
#define maxn 100010
using namespace std;
int n, a[maxn], k[maxn], ans;
pair<int, int> dp[maxn], dp1[300], dp0[maxn];
int main(){
ios_base::sync_with_stdio(0);
cout.tie(0);
cin.tie(0);
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){
for(int i = 0; i < n; ++i){
dp[i] = {1, -1};
for(int j = 0; j < i; ++j){
if(__builtin_popcount(a[i]&a[j]) == k[i]){
if(dp[j].first+1 > dp[i].first){
dp[i] = dp[j];
dp[i].second = j;
dp[i].first = dp[j].first+1;
}
}
}
}
pair<int, int> ans = {1, -1};
int idx = 0;
for(int i = 0; i < n; ++i){
ans = max(ans, dp[i]);
if(ans == dp[i]){
idx = i;
}
}
vector<int> res;
while(true){
res.push_back(idx+1);
if(ans.second == -1){
break;
}
idx = ans.second;
ans = dp[ans.second];
}
reverse(res.begin(), res.end());
cout << res.size() << '\n';
for(auto tmp:res){
cout << tmp<<'\n';
}
return 0;
}
for(int j = 0; j <= (1<<8); ++j){
dp1[j] = {0, -1};
}
for(int i = 0; i < n; ++i){
dp0[i] = {1, -1};
for(int j = 0; j <= (1<<8); ++j){
if(__builtin_popcount(j&a[i]) == k[i]){
if(dp1[j].first+1 > dp0[i].first){
dp0[i] = dp1[j];
dp0[i].first = dp1[j].first+1;
}
}
}
if(dp0[i].first > dp1[a[i]].first){
dp1[a[i]] = dp0[i];
dp1[a[i]].second = i;
}
}
pair<int, int> ans = {1, -1};
int idx = 0;
for(int i = 0; i < n; ++i){
ans = max(ans, dp0[i]);
if(ans == dp0[i]){
idx = i;
}
}
vector<int> res;
while(true){
res.push_back(idx+1);
if(ans.second == -1){
break;
}
idx = ans.second;
ans = dp0[ans.second];
}
reverse(res.begin(), res.end());
cout << res.size() << '\n';
for(auto tmp:res){
cout << tmp<<'\n';
}
}
# | 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... |