Submission #1098773

#TimeUsernameProblemLanguageResultExecution timeMemory
1098773vjudge1Longest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
3284 ms92500 KiB
#include <bits/stdc++.h>

#define ll long long
#define ldb long double
#define endl '\n'
#define For(i,l,r) for(int i=l;i<=r;i++)
#define ForD(i,r,l) for(int i=r;i>=l;i--)
#define REP(i,l,r) For(i,l,r-1)
#define PER(i,r,l) ForD(i,r-1,l)
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
#define All(x,n) x+1,x+1+n
#define Alll(x,n) x,x+n
#define sz(x) (signed)x.size()
#define unq(x) x.resize(unique(all(x))-x.begin())
#define mpa make_pair
#define LF 10
#define RG 10
#define getLeft(x) x&((1<<LF)-1)
#define getRight(x) x>>LF

#ifdef NCGM
#include"debug.h"
#else 
#define debug(...) "fr";
#endif

using namespace std;


const int N=1e5+3;

int n,pre[1<<LF][1<<RG][LF+1],pre1[1<<LF][1<<RG][LF+1];
int a[N],k[N],f[N],op[N];

void mxm(int &x,int y) {
    x=max(x,y);
}
int main() {
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    For(i,1,n) cin >> a[i];
    For(i,1,n) cin >> k[i];
    For(i,0,(1<<RG)-1)
        For(j,0,(1<<LF)-1)
            For(k,0,LF) pre[j][i][k]=pre1[j][i][k]=0;
    // debug(pre[])
    For(i,1,n) {
        f[i]=1;
        op[i]=0;
        For(j,0,(1<<RG)-1) {
            int cnt=__builtin_popcount(j&getRight(a[i]));
            if (cnt>k[i] || k[i]-cnt>LF) continue;
            if (pre[getLeft(a[i])][j][k[i]-cnt]+1>f[i]) {
                f[i]=pre[getLeft(a[i])][j][k[i]-cnt]+1;
                op[i]=pre1[getLeft(a[i])][j][k[i]-cnt];
            }
        }
        For(j,0,(1<<LF)-1) {
            if (pre[j][getRight(a[i])][__builtin_popcount(j&getLeft(a[i]))]<f[i]) {
                pre[j][getRight(a[i])][__builtin_popcount(j&getLeft(a[i]))]=f[i];
                pre1[j][getRight(a[i])][__builtin_popcount(j&getLeft(a[i]))]=i;
            }
        }
    }
    int ans=0,lst=-1;
    For(i,1,n) 
        if (f[i]>ans) ans=f[i],lst=i;
    vector<int> track;
    while(lst!=0) {
        track.pb(lst);
        lst=op[lst];
    }
    cout << sz(track) << endl;
    reverse(all(track));
    for(auto el: track) cout << el << " ";
    // cout << ans << endl;
    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...