Submission #1325041

#TimeUsernameProblemLanguageResultExecution timeMemory
1325041tuncay_pashaLongest beautiful sequence (IZhO17_subsequence)C++20
40 / 100
6091 ms19916 KiB
/**
 * author:  tuncypasha
 * file:    b.cpp
 * created: 05.02.2026 17:16
**/
#include <bits/stdc++.h>
#define pasha ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(v) begin(v), end(v)
using namespace std;

// mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

constexpr int N = 2e5 + 5, oo = 1e16;

void _() {
  int n;
  cin >> n;
  vector<int> a(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> a[i];
  vector<int> k(n + 1);
  for (int i = 1; i <= n; ++i)
    cin >> k[i];
  if (n <= 5000) {
    vector<int> dp(n + 1, 1);
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j < i; ++j) {
        if (__builtin_popcount(a[i] & a[j]) == k[i])
          dp[i] = max(dp[i], dp[j] + 1);
      }
    }
    int j = 0;
    for (int i = 1; i <= n; ++i) {
      if (dp[i] >= dp[j])
        j = i;
    }
    vector<int> ans;
    ans.pb(j);
    while (dp[j] > 1) {
      for (int i = 1; i < j; ++i) {
        if (dp[i] + 1 == dp[j] && __builtin_popcount(a[i] & a[j]) == k[j]) {
          j = i;
          break;
        }
      }
      ans.pb(j);
    }
    cout << ans.size() << '\n';
    reverse(all(ans));
    for (int &i : ans)
      cout << i << ' ';
    return cout << '\n', void();
  }
  int mx = *max_element(a.begin() + 1, a.end());
  vector<int> par(n + 1, -1);
  vector<int> dp(n + 1, 1);
  vector<pair<int, int>> dpmx(mx + 1, {-1, -1});
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j <= mx; ++j) {
      if (dpmx[j].ss == -1 || __builtin_popcount(a[i] & j) != k[i])
        continue;
      if (dpmx[j].ff + 1 >= dp[i]) {
        par[i] = dpmx[j].ss;
        dp[i] = dpmx[j].ff + 1;
      }
    }
    dpmx[a[i]] = max(dpmx[a[i]], {dp[i], i});
  }
  int j = 0;
  for (int i = 1; i <= n; ++i) {
    if (dp[i] >= dp[j])
      j = i;
  }
  vector<int> ans;
  ans.pb(j);
  while (par[j] != -1) {
    j = par[j];
    ans.pb(j);
  }
  cout << ans.size() << '\n';
  reverse(all(ans));
  for (int &i : ans)
    cout << i << ' ';
  cout << '\n', void();
}

signed main() {
  pasha
  int t = 1;
  // cin >> t;
  for (int cs = 1; cs <= t; ++cs) {
    _();
    // cout << '\n';
  }
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...