Submission #1189563

#TimeUsernameProblemLanguageResultExecution timeMemory
1189563SalihSahinLongest beautiful sequence (IZhO17_subsequence)C++20
23 / 100
6092 ms3548 KiB
#include "bits/stdc++.h"
#define pb push_back
#define int long long
using namespace std;

const int N = 1e2;
const int inf = 1e17;

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.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];
    }

    vector<int> dp(n, 1), par(n);
    int ans = 0, mxind = -1;
    for(int i = 0; i < n; i++){
        par[i] = i;

        for(int j = i-1; j >= 0; j--){
            if(__builtin_popcount(a[i]&a[j]) == k[i]){
                if(dp[j] + 1 > dp[i]){
                    dp[i] = dp[j] + 1;
                    par[i] = j;
                }
            }
        }
        if(ans < dp[i]){
            ans = dp[i];
            mxind = i;
        }
    }
    cout<<ans<<endl;
    vector<int> ansarr;
    while(par[mxind] != mxind){
        ansarr.pb(mxind);
        mxind = par[mxind];
    }
    ansarr.pb(mxind);
    reverse(ansarr.begin(), ansarr.end());
    for(int i = 0; i < ans; i++){
        cout<<ansarr[i] + 1<<" ";
    }
    cout<<endl;
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...