Submission #769547

#TimeUsernameProblemLanguageResultExecution timeMemory
769547asdfgraceLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
119 ms4476 KiB
#include <bits/stdc++.h>
using namespace std;
#define DEBUG(x) //x
#define A(x) DEBUG(assert(x))
#define PRINT(x) DEBUG(cerr << x)
#define PV(x) DEBUG(cerr << #x << " = " << x << '\n')
#define PV2(x) DEBUG(cerr << #x << " = " << x.first << ',' << x.second << '\n')
#define PAR(x) DEBUG(PRINT(#x << " = { "); for (auto y : x) PRINT(y << ' '); PRINT("}\n");)
#define PAR2(x) DEBUG(PRINT(#x << " = { "); for (auto [y, z] : x) PRINT(y << ',' << z << "  "); PRINT("}\n");)
#define PAR2D(x) DEBUG(PRINT(#x << ":\n"); for (auto arr : x) {PAR(arr);} PRINT('\n'));
using i64 = long long;
const int INF = 1000000007; //998244353;

struct S {
  void run() {
    int n; cin >> n;
    vector<int> a(n), k(n);
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> k[i];
    }
    vector<pair<int, int>> dp(n);
    if (n <= 5000) {
      for (int i = 0; i < n; ++i) {
        dp[i] = {1, i};
        for (int j = 0; j < i; ++j) {
          if (__builtin_popcount(a[i] & a[j]) == k[i]
            && dp[j].first + 1 > dp[i].first) {
            dp[i].first = dp[j].first + 1;
            dp[i].second = j;
          }
        }
      }
    } else {
      vector<pair<int, int>> best(1 << 8, {0, 0});
      for (int i = 0; i < n; ++i) {
        dp[i] = {1, i};
        for (int j = 0; j < (1 << 8); ++j) {
          if (__builtin_popcount(a[i] & j) != k[i]) continue;
          if (best[j].first + 1 > dp[i].first) {
            dp[i].first = best[j].first + 1;
            dp[i].second = best[j].second;
          }
        }
        if (dp[i].first > best[a[i]].first) {
          best[a[i]].first = dp[i].first;
          best[a[i]].second = i;
        }
      }
    }
    int ans = 0, last = 0;
    for (int i = 0; i < n; ++i) {
      if (dp[i].first > ans) {
        ans = dp[i].first;
        last = i;
      }
    }
    int cur = last, next = dp[last].second;
    vector<int> v(1, cur);
    while (next != cur) {
      v.push_back(next);
      cur = next;
      next = dp[next].second;
    }
    reverse(v.begin(), v.end());
    cout << v.size() << '\n';
    for (int i = 0; i < (int)v.size(); ++i) {
      cout << v[i] + 1 << (i == (int)v.size() - 1 ? '\n' : ' ');
    }
  }
};

int main() {
  ios::sync_with_stdio(0); cin.tie(0);
  auto sol = make_unique<S>();
  sol->run();
}


#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...