제출 #170585

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

using namespace std;

typedef long long ll;

const int N = 5007;

int dp[N], p[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];
    }
    for (int i = 0; i < n; i++) {
        cin >> k[i];
        dp[i] = 1;
        p[i] = -1;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < i; j++) {
            if (__builtin_popcount(a[i] & a[j]) == k[i]) {
                dp[i] = max(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...