Submission #1135274

#TimeUsernameProblemLanguageResultExecution timeMemory
1135274bleahbleahLongest beautiful sequence (IZhO17_subsequence)C++20
100 / 100
5798 ms170288 KiB

#include <bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
using namespace std;

//#ifndef DLOCAL
   //#define cin fin
   //#define cout fout
   //ifstream cin("subsequence.in");
   //ofstream cout("subsequence.out");
//#endif

using ll = long long;
using ld = long double;

//#define int ll
#define sz(x) ((int)(x).size())

using pii = pair<int,int>;
using tii = tuple<int,int,int>;

pii dp[1 << 20][20];
int ppc[1 << 20];

const int nmax = 2e5 + 5;
int lst[nmax], sum[nmax];
int v[nmax], k[nmax];

signed main() {
   for(int i = 0; i < (1 << 20); i++) ppc[i] = ppc[i >> 1] + (i & 1);
   cin.tie(0) -> sync_with_stdio(0);
   int n;
   cin >> n;
   for(int i = 1; i <= n; i++) cin >> v[i];
   for(int j = 1; j <= n; j++) cin >> k[j];
   reverse(v + 1, v + n + 1);
   reverse(k + 1, k + n + 1);
   const int B = 20001;
   pii globalbest(0, 0);
   for(int i = 1; i <= n; i++) {
      pii best = dp[v[i]][0];
      for(int j = i - 1; j % B != 0; j--) 
         if(ppc[v[j] & v[i]] == k[j]) best = max(best, make_pair(sum[j], j));
      tie(sum[i], lst[i]) = best;
      sum[i]++;
      globalbest = max(globalbest, make_pair(sum[i], i));
      
      if(i % B == 0) {
         //cerr << "wah\n";
         for(int step = 0; step < 20; step++) 
            for(int msk = 0; msk < (1 << 20); msk++) 
               dp[msk][step] = pii{0, 0};
         for(int j = 1; j <= i; j++) {
            if(k[j] > ppc[v[j]]) continue;
            dp[v[j]][k[j]] = max(dp[v[j]][k[j]], make_pair(sum[j], j));
         }
         pii a, b;
         for(int step = 0; step < 20; step++) {
            for(int msk = 0; msk < (1 << 20); msk++) {
               if(msk & (1 << step)) continue;
               for(int h = 0; h < 19; h++) {
                  a = dp[msk][h], b = dp[msk | (1 << step)][h];
                  dp[msk][h] = max(a, b);
                  dp[msk | (1 << step)][h] = max(a, dp[msk | (1 << step)][h + 1]);
               }
            }
         }
      }
   }
   
   cout << globalbest.first << '\n';
   vector<int> vo;
   while(globalbest.second != 0) {
      vo.emplace_back(globalbest.second);
      globalbest.second = lst[globalbest.second];
   }
   for(auto &x : vo) x = n + 1 - x, cout << x << ' ';
   cout << '\n';
}

/**
      Binecuvanteaza Doamne Ukraina.
--
*/ 
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...