제출 #936111

#제출 시각아이디문제언어결과실행 시간메모리
93611112345678Longest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
99 ms6236 KiB
#include <bits/stdc++.h>

using namespace std;

const int nx=1e5+5;
int n, v[nx], k[nx];
pair<int, int> t, vl[nx], dp[nx];

int main()
{
    cin.tie(NULL)->sync_with_stdio(false);
    cin>>n;
    for (int i=1; i<=n; i++) cin>>v[i], dp[i]={1, i};
    for (int i=1; i<=n; i++) cin>>k[i];
    for (int i=0; i<256; i++) vl[i]={-1, -1};
    for (int i=1; i<=n; i++)
    {
        if (n<=5000)
        {
            for (int j=i-1; j>=1; j--) if (__builtin_popcount(v[j]&v[i])==k[i]) dp[i]=max(dp[i], {dp[j].first+1, j});
            t=max(t, {dp[i].first, i});
            continue;
        }
        for (int j=0; j<256; j++)
        {
            if (vl[j].first==-1) continue;
            if (__builtin_popcount(v[i]&j)==k[i]) dp[i]=max(dp[i], {vl[j].first+1, vl[j].second});
        }
        vl[v[i]]=max(vl[v[i]], {dp[i].first, i});
        t=max(t, {dp[i].first, i});
    }
    cout<<t.first<<'\n';
    vector<int> res;
    while (dp[t.second].second!=t.second) res.push_back(t.second), t.second=dp[t.second].second;
    res.push_back(t.second);
    reverse(res.begin(), res.end());
    for (auto x:res) cout<<x<<' ';
}

/*
4
1 2 3 4
10 0 1 0

5
5 3 5 3 5
10 1 20 1 20
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...