Submission #1339414

#TimeUsernameProblemLanguageResultExecution timeMemory
1339414i_love_springLongest beautiful sequence (IZhO17_subsequence)C++20
Compilation error
0 ms0 KiB
#include <bits/stdc++.h>

using namespace std;

#define ar array
#define ll long long 
const int inf = 2e9;

const int B = 10;
struct State {
    int len = 0;
    int end = -1;
} dp[1028][1028][21];


int count_bits(int x, int y) {
    return __builtin_popcount((x & y));
}

void solve() {
    int n;
    cin >> n;
    vector<int> a(n + 1), b(n + 1);
    for (int i = 1; i <= n;i++) 
        cin >> a[i];
    
    for (int i = 1; i <= n;i++)
        cin >> b[i];

    int ans = 0, pos = -1;
    vector<int> prv(n + 1, -1);
    for (int i = 1; i <= n; i++) {
        
        int l = a[i] & ((1 << B) - 1);
        int r = a[i] >> B;
        int lbs = 1;

        int k = b[i];
        for (int l1 = 0; l1 < (1 << B); l1++) {
            int need = k - count_bits(l, l1);
            
            if (need < 0 || need > B)
                continue;
            
            if (dp[l1][r][need].len + 1 > lbs) {
                lbs = dp[l1][r][need].len + 1;
                prv[i] = dp[l1][r][need].end;
            }
        }

        if (lbs > ans) {
            ans = lbs;
            pos = i;
        }

        for (int r1 = 0; r1 < (1 << B); r1++) {
            k = count_bits(r1, r);
            if (lbs > dp[l][r1][k].len) {
                dp[l][r1][k].len = lbs;
                dp[l][r1][k].end = i;
            }
        }

    }



    vector<int> res = {pos};
    while (prv[res.back()] != -1) 
        res.push_back(prv[res.back()]);

    reverse(res.begin(), res.end());
    cout << res.size() << "\n";
    for (int x : res) 
        cout << x << " ";
    
}

int32_t main() { 

#ifdef Behruz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif 

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    solve();

    return 0;
}