제출 #882713

#제출 시각아이디문제언어결과실행 시간메모리
882713Sokol080808Longest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
2940 ms48216 KiB
#ifdef ONPC
    #define _GLIBCXX_DEBUG
#endif

#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
#include <malloc.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()

using ll = long long;
using ull = unsigned long long;
using ld = long double;

template<typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) {in >> p.first >> p.second; return in;}
template<typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2>& p) {out << p.first << ' ' << p.second; return out;}
template<typename T> istream& operator>>(istream& in, vector<T>& v) {for (auto& x : v) in >> x; return in;}
template<typename T> ostream& operator<<(ostream& out, vector<T>& v) {for (auto& x : v) out << x << ' '; return out;}
template<typename T> ostream& operator<<(ostream& out, set<T>& v) {for (auto& x : v) out << x << ' '; return out;}
template<typename T> ostream& operator<<(ostream& out, multiset<T>& v) {for (auto& x : v) out << x << ' '; return out;}

const int MAX_INT = 2147483647;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int best[1024][1024][11];
int a[100001], k[100001], dp[100001], prv[100001];
int bit_cnt[1 << 10];

int mx(int a, int b) {
    if (a == -1) return b;
    if (b == -1) return a;
    return (dp[a] > dp[b] ? a : b);
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    fill(dp, dp + 100001, 1);
    fill(prv, prv + 100001, -1);
    for (int i = 0; i < 1024; i++) for (int j = 0; j < 1024; j++) for (int k = 0; k <= 10; k++) best[i][j][k] = -1;

    for (int i = 1; i < (1 << 10); i++) bit_cnt[i] = (i & 1) + bit_cnt[i >> 1];

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

    for (int i = 1; i <= n; i++) {
        int l = a[i] / 1024, r = a[i] % 1024;
        for (int prv_r = 0; prv_r < 1024; prv_r++) {
            int cur = k[i] - bit_cnt[prv_r & r];
            if (cur < 0 || 10 < cur) continue;
            prv[i] = mx(prv[i], best[l][prv_r][cur]);
        }
        if (prv[i] != -1) dp[i] = dp[prv[i]] + 1;
        for (int nxt_l = 0; nxt_l < 1024; nxt_l++) {
            int cur = bit_cnt[l & nxt_l];
            best[nxt_l][r][cur] = mx(best[nxt_l][r][cur], i);
        }
    }


    int id = max_element(dp + 1, dp + n + 1) - dp;
    vector<int> ans;
    for (int i = dp[id]; i >= 1; i--) {
        ans.push_back(id);
        id = prv[id];
    }
    reverse(all(ans));
    cout << ans.size() << '\n';
    cout << ans << '\n';

    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...