Submission #173508

#TimeUsernameProblemLanguageResultExecution timeMemory
173508srvltLongest beautiful sequence (IZhO17_subsequence)C++14
100 / 100
4428 ms48880 KiB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree <pair <int, int>, null_type, less <pair <int, int> >, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define low_b lower_bound

#define endl "\n"
//#define int long long

using namespace std;

void dout() {
    cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;

const int N = 1e5 + 3, K = 20, H = 10;
int n, a[N], k[N], dp[N], p[N];
int mx[H + 1][(1 << H)][(1 << H)];

int getmax(int x, int y) {
    if (x == -1) {
        return y;
    }
    if (y == -1) {
        return x;
    }
    if (dp[x] > dp[y]) {
        return x;
    }
    return y;
}

vector <int> v;

void getans(int x) {
    if (x == -1) {
        return;
    }
    v.pb(x);
    getans(p[x]);
}

void solve(int tc) {
    // check for (int i = 0; i < n; j++)
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++) {
        cin >> k[i];
    }
    int mask = (1 << H);
    int res, cnt, f, s;
    memset(mx, -1, sizeof(mx));
    for (int i = 1; i <= n; i++) {
        res = -1;
        f = a[i] >> H;
        s = a[i] & (mask - 1);
        for (int j = 0; j < mask; j++) {
            cnt = k[i] - __builtin_popcount(f & j);
            if (cnt < 0 || cnt > H) {
                continue;
            }
            res = getmax(res, mx[cnt][j][s]);
        }
        if (res != -1) {
            dp[i] = dp[res];
        }
        dp[i]++;
        p[i] = res;

        for (int j = 0; j < mask; j++) {
            cnt = __builtin_popcount(s & j);
            mx[cnt][f][j] = getmax(mx[cnt][f][j], i);
        }
    }
    int ans = -1;
    for (int i = 1; i <= n; i++) {
        ans = getmax(ans, i);
    }
    cout << dp[ans] << endl;
    getans(ans);
    reverse(all(v));
    for (auto i : v) {
        cout << i << " ";
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int tc = 1;
//    cin >> tc;
    for (int i = 0; i < tc; i++) {
        solve(i);
//        cleanup();
    }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...