제출 #1314190

#제출 시각아이디문제언어결과실행 시간메모리
1314190tredsused70Longest beautiful sequence (IZhO17_subsequence)C++20
0 / 100
71 ms172792 KiB
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,avx,avx2")
//#pragma GCC optimize("trapv")
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define mpr make_pair
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int nmax = 100001, mod = 1000000007, inf = 2000010000, key = 200003, lg = 20, block = 300;
const ll infll = 4000000000000000000;
const ld eps = 1e-9;

int pr[nmax];
array<int, 2> max_seq[1 << 10][1 << 10][21];
int nums[nmax], bits[nmax];

void solve()
{
    int n;
    cin >> n;
    for(int i = 1; i <= n; i++)
        cin >> nums[i];
    for(int i = 0; i < 1024; i++)
        for(int j = 0; j < 1024; j++)
            for(int k = 0; k <= 20; k++)
                max_seq[i][j][k] = {0, -1};
    int ans = 0, last_ind = -1;
    for(int i = 1; i <= n; i++) {
        cin >> bits[i];
        int first_half = nums[i] & (1023);
        int second_half = (nums[i] >> 10) << 10;
        array<int, 2> prev_max = {0, -1};
        for(int j = 0; j < 1024; j++) {
            int cnt_first = __builtin_popcount(first_half & j);
            int cnt_req = bits[i] - cnt_first;
            if(cnt_req < 0)
                continue;
            prev_max = max(prev_max, max_seq[j][second_half][cnt_req]);
        }
        pr[i] = prev_max[1];
        if(prev_max[0] + 1 > ans) {
            ans = prev_max[0] + 1;
            last_ind = i;
        }
        array<int, 2> cur_max = {prev_max[0] + 1, i};
        for(int j = 0; j < 1024; j++) {
            int cnt_second = __builtin_popcount(second_half & j);
            max_seq[first_half][j][cnt_second] = max(max_seq[first_half][j][cnt_second], cur_max);
        }
    }
    cout << ans << "\n";
    vector<int> seq;
    seq.pb(last_ind);
    for(int i = 1; i < ans; i++) {
        last_ind = pr[last_ind];
        seq.pb(last_ind);
    }
    reverse(all(seq));
    for(int i : seq)
        cout << i << " ";
    cout << "\n";
    return ;
}

int main()
{
    //freopen("movie.in","r",stdin);
    //freopen("movie.out","w",stdout);
    ios_base::sync_with_stdio(0);cin.tie(0);
    srand(8713);
    //init();
    int t = 1;
    //cin >> t;
    //int t_ = t;
    //t = rdi();
    while(t--) {
        //cout << "Case #" << t_ - t << ": ";
        solve();
    }
    //flush();
    return 0;
}

/*
9
5 3 1 5 7 8 6 9 7
3
3 C
7 C
5 W
*/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...