Submission #854092

#TimeUsernameProblemLanguageResultExecution timeMemory
854092The_SamuraiLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
110 ms2708 KiB
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int inf = 1e9;

int main() {
    cin.tie(0)->sync_with_stdio(false);
#ifdef sunnatov
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("subsequence.in", "r", stdin);
    //freopen("subsequence.out", "w", stdout);
#endif

    int n;
    cin >> n;
    vector<int> a(n + 1), k(n + 1);
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++) cin >> k[i];

    if (n <= 1e5 and *max_element(a.begin(), a.end()) <= (1 << 8)) {
        const int N = (1 << 8) + 1;
        vector<int> dp(n + 1, 1), par(n + 1), last_pos(N, -1);
        last_pos[a[1]] = 1;
        for (int i = 2; i <= n; i++) {
            for (int j = 0; j < N; j++) {
                if (last_pos[j] == -1) continue;
                if (__builtin_popcount(a[i] & a[last_pos[j]]) == k[i] and dp[i] < dp[last_pos[j]] + 1) {
                    dp[i] = dp[last_pos[j]] + 1;
                    par[i] = last_pos[j];
                }
            }
            if (last_pos[a[i]] == -1 or dp[last_pos[a[i]]] < dp[i]) last_pos[a[i]] = i;
        }
        vector<int> way = {(int) (max_element(dp.begin() + 1, dp.end()) - dp.begin())};
        while (dp[way.back()] != 1) way.emplace_back(par[way.back()]);
        reverse(way.begin(), way.end());
        cout << way.size() << '\n';
        for (int x: way) cout << x << ' ';
        return 0;
    }
    if (n <= 5000 and *max_element(a.begin(), a.end()) <= (1 << 20)) {
        vector<int> dp(n + 1, 1), par(n + 1);
        for (int i = 2; i <= n; i++) {
            for (int j = 1; j < i; j++) {
                if (__builtin_popcount(a[i] & a[j]) == k[i] and dp[i] < dp[j] + 1) {
                    dp[i] = dp[j] + 1;
                    par[i] = j;
                }
            }
        }
        vector<int> way = {(int) (max_element(dp.begin() + 1, dp.end()) - dp.begin())};
        while (dp[way.back()] != 1) way.emplace_back(par[way.back()]);
        reverse(way.begin(), way.end());
        cout << way.size() << '\n';
        for (int x: way) cout << x << ' ';
        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...