제출 #170591

#제출 시각아이디문제언어결과실행 시간메모리
170591mcdx9524Longest beautiful sequence (IZhO17_subsequence)C++14
0 / 100
2 ms504 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 1e5 + 7;

int dp[N], p[N];
int ndp[N], npos[N];

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    vector <int> a(n), k(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        ndp[a[i]] = 1;
        if (!npos[a[i]]) {
            npos[a[i]] = i;
        }
    }
    for (int i = 0; i < n; i++) {
        cin >> k[i];
        dp[i] = 1;
        p[i] = -1;
    }
    if (n > 5000) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 256; j++) {
                if (__builtin_popcount(a[i] & j) == k[i]) {
                    if (ndp[j] >= dp[i]) {
                        dp[i] = ndp[j] + 1;
                        p[i] = npos[j];
                    }
                }
            }
            if (dp[i] >= ndp[a[i]]) {
                ndp[a[i]] = dp[i];
                npos[a[i]] = i;
            }
        }
        int ans = 0;
        for (int i = 0; i < n; i++) {
            ans = max(ans, dp[i]);
        }
        vector <int> res;
        for (int i = 0; i < n; i++) {
            if (dp[i] == ans) {
                while (i != -1) {
                    res.push_back(i);
                    i = p[i];
                }
                break;
            }
        }
        reverse(res.begin(), res.end());
        cout << ans << '\n';
        for (int x : res) {
            cout << x + 1 << ' ';
        }
        return 0;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (__builtin_popcount(a[i] & a[j]) == k[i]) {
                if (dp[j] >= dp[i]) {
                    dp[i] = dp[j] + 1;
                    p[i] = j;
                }
            }
        }
    }
    int ans = 0;
    for (int i = 0; i < n; i++) {
        ans = max(ans, dp[i]);
    }
    vector <int> res;
    for (int i = 0; i < n; i++) {
        if (dp[i] == ans) {
            while (i != -1) {
                res.push_back(i);
                i = p[i];
            }
            break;
        }
    }
    reverse(res.begin(), res.end());
    cout << ans << '\n';
    for (int x : res) {
        cout << x + 1 << ' ';
    }
    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...