제출 #172954

#제출 시각아이디문제언어결과실행 시간메모리
172954VEGAnnLongest beautiful sequence (IZhO17_subsequence)C++14
100 / 100
5856 ms93580 KiB
#include <bits/stdc++.h>
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC optimize ("-O3")
//#pragma GCC optimize ("Ofast")
#define sz(x) ((int)x.size())
#define PB push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N = 100100;
//const int N2 = N * N;
const int PW = 10;
const int VL = (1 << PW);
const int oo = 2e9;
const int K = 11;
vector<int> vc;
int n, a[N], k[N], pr[N], f[N], ans = 0, nm = -1, ff[VL][VL][K], loc[VL][VL][K];

bool ok(int x, int y){
    return (__builtin_popcount(a[x] & a[y]) == k[y]);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
//    freopen("in.txt","r",stdin);

    cin >> n;
    bool okk = 1;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        okk &= (a[i] < VL);
    }
    for (int i = 0; i < n; i++)
        cin >> k[i];

    for (int i = 0; i < n; i++) {
        f[i] = 1;

        int fi = a[i] % VL, se = (a[i] >> 10);

        for (int pf = 0; pf < VL; pf++){
            int kl = k[i] - __builtin_popcount(pf & fi);
            if (kl >= 0 && kl < 11 && f[i] < ff[pf][se][kl] + 1){
                f[i] = ff[pf][se][kl] + 1;
                pr[i] = loc[pf][se][kl];
            }
        }

        for (int ns = 0; ns < VL; ns++){
            int kl =  __builtin_popcount(ns & se);
            if (ff[fi][ns][kl] < f[i]){
                ff[fi][ns][kl] = f[i];
                loc[fi][ns][kl] = i;
            }

        }

        if (ans < f[i]){
            ans = f[i];
            nm = i;
        }
    }

    for (;;){
        vc.PB(nm + 1);
        if (f[nm] == 1) break;
        nm = pr[nm];
    }

    reverse(all(vc));
    cout << sz(vc) << '\n';
    for (int x : vc)
        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...