Submission #677791

#TimeUsernameProblemLanguageResultExecution timeMemory
677791TigryonochekkLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
148 ms4100 KiB
#include <iostream>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int, int>

const int N = 1e5 + 2, A = 256, LA = 10;

int n;
int a[N], k[N];
int dp[N];
int p[N];

pii len[A];
bool x[A][A][LA];

int bc(int n) {
    int cnt = 0;
    while (n) {
        n &= (n - 1);
        cnt++;
    }
    return cnt;
}

void printAns(int i) {
    if (dp[i] == 1) {
        cout << i << " ";
        return;
    }
    printAns(p[i]);
    cout << i << " ";
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> k[i];
    }
    if (n <= 5000) {
        int ans = 1;
        dp[1] = 1;
        p[1] = 0;
        for (int i = 2; i <= n; i++) {
            dp[i] = 1;
            p[i] = 0;
            for (int j = 1; j < i; j++) {
                if (bc(a[i] & a[j]) == k[i]) {
                    if (dp[i] < dp[j] + 1) {
                        dp[i] = dp[j] + 1;
                        p[i] = j;
                    }
                    ans = max(ans, dp[i]);
                }
            }
        }
        cout << ans << endl;
        for (int i = n; i >= 1; i--) {
            if (dp[i] == ans) {
                printAns(i);
                cout << endl;
                return 0;
            }
        }
    }
    else {
        for (int i = 0; i < A; i++) {
            for (int j = 0; j < A; j++) {
                x[i][j][bc(i & j)] = 1;
            }
        }
        int ans = 1;
        dp[1] = 1;
        p[1] = 0;
        len[a[1]] = pii(1, 1);
        for (int i = 2; i <= n; i++) {
            dp[i] = 1;
            p[i] = 0;
            if (k[i] > bc(a[i])) {
                dp[i] = 1;
                ans = max(ans, dp[i]);
                len[a[i]] = max(len[a[i]], pii(dp[i], i));
                continue;
            }

            for (int b = 0; b < A; b++) {
                if (x[b][ a[i] ][ k[i] ]) {
                    if (len[b].first) {
                        int j = len[b].second;
                        if (dp[i] < dp[j] + 1) {
                            dp[i] = dp[j] + 1;
                            p[i] = j;
                        }
                    }
                }
            }

            ans = max(ans, dp[i]);
            len[a[i]] = max(len[a[i]], pii(dp[i], i) );
        }

        cout << ans << endl;
        for (int i = n; i >= 1; i--) {
            if (dp[i] == ans) {
                printAns(i);
                cout << endl;
                return 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...