Submission #665224

#TimeUsernameProblemLanguageResultExecution timeMemory
665224finn__Longest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
5891 ms93320 KiB
#include <bits/stdc++.h>
using namespace std;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    size_t n;
    cin >> n;

    vector<unsigned> a(n), k(n);
    for (unsigned &x : a)
        cin >> x;
    for (unsigned &x : k)
        cin >> x;

    // dp[i][j][k] contains the last index of the LBS, where the last element has
    // a first half of i, the next element has a second element of j and
    // requires k bit in common with j.
    vector<vector<vector<unsigned>>>
        dp(1 << 10, vector<vector<unsigned>>(1 << 10, vector<unsigned>(11, UINT_MAX)));
    vector<unsigned> lbs(n, 0), pre(n, UINT_MAX);
    unsigned longest_end = 0;

    for (size_t i = 0; i < n; i++)
    {
        unsigned const p = a[i] >> 10;
        unsigned const q = a[i] ^ (p << 10);
        size_t best_pre = UINT_MAX;

        for (unsigned j = 0; j < 1 << 10U; j++)
        {
            unsigned req = k[i] - __builtin_popcount(p & j);
            if (req < 11 && dp[j][q][req] != UINT_MAX &&
                (best_pre == UINT_MAX || lbs[best_pre] < lbs[dp[j][q][req]]))
                best_pre = dp[j][q][req];
        }

        lbs[i] = best_pre == UINT_MAX ? 1 : lbs[best_pre] + 1;
        pre[i] = best_pre;
        if (lbs[i] > lbs[longest_end])
            longest_end = i;

        for (unsigned j = 0; j < 1 << 10U; j++)
        {
            unsigned const c = __builtin_popcount(j & q);
            if (dp[p][j][c] == UINT_MAX || lbs[i] > lbs[dp[p][j][c]])
                dp[p][j][c] = i;
        }
    }

    cout << lbs[longest_end] << '\n';
    size_t i = longest_end;
    vector<unsigned> seq;
    while (i != UINT_MAX)
    {
        seq.push_back(i + 1);
        i = pre[i];
    }
    for (auto it = seq.rbegin(); it != seq.rend(); it++)
        cout << *it << ' ';
    cout << '\n';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...