Submission #672196

#TimeUsernameProblemLanguageResultExecution timeMemory
672196Alihan_8Longest beautiful sequence (IZhO17_subsequence)C++17
23 / 100
6082 ms53104 KiB
#include <bits/stdc++.h>
// include <ext/pb_ds/assoc_container.hpp>
// include <ext/pb_ds/tree_policy.hpp>
// using namespace __gnu_pbds;
using namespace std;
#define all(x) x.begin(), x.end()
#define pb push_back
// define ordered_set tree<int,null_type,less_equal<int>,rb_tree_tag,tree_order_statistics_node_update>
#define mpr make_pair
#define ln '\n'
void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
#define int long long
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int n; cin >> n;
    vector <int> p(n), k(n);
    for ( auto &i: p ) cin >> i;
    for ( auto &i: k ) cin >> i;
    vector <int> dp(n), par(n, -1);
    auto f = [&](int l, int r){
        return __builtin_popcount(l & r);
    };
    if ( n <= 5000 ){
        for ( int i = 0; i < n; i++ ){
            dp[i] = 1;
            for ( int j = 0; j < i; j++ ){
                if ( f(p[i], p[j]) == k[i] ){
                    if ( dp[i] < dp[j]+1 ){
                        dp[i] = dp[j]+1;
                        par[i] = j;
                    }
                }
            }
        }
    }
    else{
        const int S = 1 << 20;
        multiset<pair<int,int>> g[S];
        for ( int i = 0; i < n; i++ ){
            dp[i] = 1;
            for ( int mask = 0; mask < S; mask++ ){
                if ( f(mask, p[i]) == k[i] ){
                    if ( g[mask].empty() ) continue;
                    auto [val, it] = *g[mask].rbegin();
                    if ( val+1 <= dp[i] ) continue;
                    dp[i] = val+1;
                    par[i] = it;
                }
            }
            g[p[i]].insert({dp[i], i});
        }
    }
    int it = -1;
    for ( int i = 0; i < n; i++ ){
        if ( it == -1 or dp[it] < dp[i] ) it = i;
    }
    vector <int> res;
    while ( it != -1 ){
        res.pb(it);
        it = par[it];
    }
    reverse(all(res));
    cout << (int)res.size() << ln;
    for ( auto i: res ) cout << i+1 << ' ';

    cout << '\n';
}

Compilation message (stderr)

subsequence.cpp: In function 'void IO(std::string)':
subsequence.cpp:11:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                      ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subsequence.cpp:11:70: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   11 | void IO(string name){freopen((name+".in").c_str(),"r",stdin); freopen((name+".out").c_str(),"w",stdout);}
      |                                                               ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...