Submission #1322480

#TimeUsernameProblemLanguageResultExecution timeMemory
1322480chithanhnguyenLongest beautiful sequence (IZhO17_subsequence)C++20
23 / 100
36 ms1080 KiB
/*
Author: Nguyen Chi Thanh - High School for the Gifted - VNU.HCM (i2528)
*/
#include <bits/stdc++.h>
using namespace std;

// #define int long long
#define ull unsigned long long
#define ld long double
#define pii pair<int, int>
#define fi first
#define se second
#define __builtin_popcount __builtin_popcountll
#define all(x) (x).begin(), (x).end()
#define BIT(x, i) (((x) >> (i)) & 1)
#define MASK(x) ((1ll << (x)))

#define debug(a, l, r) for (int _i = (l); _i <= (r); ++_i) cout << (a)[_i] << ' '; cout << '\n';

const int MAXN = 1e5 + 5;
int n, a[MAXN], k[MAXN];

void init() {
    cin >> n;
    for (int i = 1; i <= n; ++i) cin >> a[i];
    for (int i = 1; i <= n; ++i) cin >> k[i];
}

namespace subtask12 {
    bool check() {return n <= 5000;}

    void solve() {
        vector<int> dp(n + 5, 0), trace(n + 5, 0);

        int res = 0, end_pos = 0;

        for (int i = 1; i <= n; ++i) {
            dp[i] = 1;
            for (int j = 1; j < i; ++j) {
                if (__builtin_popcount(a[j] & a[i]) != k[i]) continue;
                if (dp[j] + 1 > dp[i]) {
                    dp[i] = dp[j] + 1;
                    trace[i] = j;
                }
            }

            if (dp[i] > res) {
                res = dp[i];
                end_pos = i;
            }
        }

        cout << res << '\n';
        vector<int> path;

        int cur = end_pos;
        while (cur) {
            path.push_back(cur);
            cur = trace[cur];
        }

        reverse(all(path));
        for (auto u : path) cout << u << ' ';
    }
}

signed main() {
    #ifdef NCTHANH
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    ios_base::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);

    init();
    if (subtask12::check()) return subtask12::solve(), 0;

    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...