Submission #464890

#TimeUsernameProblemLanguageResultExecution timeMemory
464890TeaTimeLongest beautiful sequence (IZhO17_subsequence)C++17
23 / 100
6043 ms2904 KiB
//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <unordered_map>
#include <bitset>

using namespace std;

#define fastInp cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

typedef long long ll;
typedef long double ld;

#define int ll
const ll INF = 1e9, SZ = 2e5 + 100, SZ2 = 510;

ll n;
vector<ll> vec, ks;
ll par[SZ], dp[SZ];

ll btCnt(ll x) {
    ll cnt = 0;
    while (x > 0) {
        if (x % 2 == 0) {
            x /= 2;
        } else {
            x--;
            cnt++;
        }
    }

    return cnt;
}
signed main() {
    fastInp;

    cin >> n;
    vec.resize(n);

    for (auto& c : vec) cin >> c;

    ks.resize(n);
    for (auto& c : ks) cin >> c;

    par[0] = -1;
    dp[0] = 1;
    pair<ll, ll> bst = { 1, 0 };
    for (int i = 1; i < n; i++) {
        par[i] = -1;
        dp[i] = 1;
        for (int j = 0; j < i; j++) {
            if (ks[i] == btCnt((vec[i]) & (vec[j]))) {
                if (dp[j] + 1 > dp[i]) {
                    dp[i] = dp[j] + 1;
                    par[i] = j;
                }
            }
        }

        bst = max(bst, make_pair(dp[i], i));
    }

    vector<ll> ans;

    while (bst.second != -1) {
        ans.push_back(bst.second);
        bst.second = par[bst.second];
    }
    reverse(ans.begin(), ans.end());
    cout << ans.size() << "\n";
    for (auto c : ans) cout << c + 1 << " ";
    
   
    return 0;
}

/*
3 4
RGWR
GRGG
RGWW

4 4
RGWR
GRRG
WGGW
WWWR

5 5
RGRGW
GRRGW
WGGWR
RWRGW
RGWGW
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...