Submission #1195471

#TimeUsernameProblemLanguageResultExecution timeMemory
1195471Yusif_NazarliLongest beautiful sequence (IZhO17_subsequence)C++20
23 / 100
6094 ms3396 KiB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define Mor_Te ios_base::sync_with_stdio(0); cin.tie(); cout.tie(0);
#define intt long long
#define pb push_back
#define gcd __gcd
#define all(v) v.begin() , v.end()

using namespace std;
using namespace __gnu_pbds;

template<typename T>
using indexed_set = tree<T , null_type , less<T> , rb_tree_tag , tree_order_statistics_node_update>;

const intt sz = 5e3 + 5;
const intt INF = 1e18;
const intt MOD = 1e9 + 7;

intt used[sz];

void Yusiff(){
    intt n; cin >> n;
    vector<intt> a(n), k(n), dp(n, 1), par(n, -1);
    for(intt i = 0;  i < n; ++i){
        cin >> a[i];
    }
    for(intt i = 0; i < n; i++){
        cin >> k[i];
    }

    for(intt i = 0; i < n; i++){
        for(intt j = 0; j < i; j++){
            if(__builtin_popcount(a[j] & a[i]) == k[i]){
                if(dp[j] + 1 > dp[i]){
                    dp[i] = dp[j] + 1;
                    par[i] = j;
                }
            }
        }
    }

    intt mx = 0, idx = -1;
    for(intt i = 0; i < n; i++){
        if(dp[i] > mx){
            mx = dp[i];
            idx = i;
        }
    }

    vector<intt> res;
    while(idx != -1){
        res.pb(idx + 1); 
        idx = par[idx];
    }
    reverse(all(res));

    cout << mx << '\n';
    for(auto x : res) cout << x << ' ';
    cout << '\n';
}

signed main(){
    Mor_Te

    intt t = 1;
    // cin >> t;
    while(t--){
        Yusiff();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...