Submission #507031

#TimeUsernameProblemLanguageResultExecution timeMemory
507031abc864197532Longest beautiful sequence (IZhO17_subsequence)C++17
23 / 100
226 ms123512 KiB
#include <bits/stdc++.h> using namespace std; #define lli long long int #define mp make_pair #define eb emplace_back #define pb push_back #define X first #define Y second #define pii pair<int, int> #define all(x) x.begin(), x.end() #define rall(x) x.rbegin(), x.rend() void abc() {cout << endl;} template <typename T, typename ...U> void abc(T i, U ...j) { cout << i << ' ', abc(j...); } template <typename T> void printv(T l, T r) { for (; l != r; ++l) cout << *l << " \n"[l + 1 == r]; } #define test(x...) abc("[" + string(#x) + "]", x); const int N = 200000; int main () { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; int C = 1000, K = 8; if (n <= 5000) C = 5000, K = 20; 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], k[i] = min(k[i], K); vector <int> dp(n, 1), bck(n, -1); vector <vector <int>> tmp(1 << K, vector <int> (K + 1, -1)); auto upd = [&](int &i, int j) { if (i == -1) i = j; else if (dp[i] < dp[j]) i = j; }; auto rebuild = [&](int to) { tmp = vector <vector <int>> (1 << K, vector <int> (K + 1, -1)); for (int i = 0; i < to; ++i) { int x = __builtin_popcount(a[i]); upd(tmp[a[i]][x], i); } for (int cnt = 1; cnt <= K; ++cnt) { for (int i = 0; i < K; ++i) { for (int s = 0; s < 1 << K; ++s) { if (s >> i & 1) { upd(tmp[s][cnt], tmp[s ^ (1 << i)][cnt - 1]); } else { upd(tmp[s][cnt], tmp[s][cnt]); } } } } }; for (int i = 0; i < n; i += C) { int to = min(i + C, n); for (int j = i; j < to; ++j) { if (tmp[a[j]][k[j]] != -1 && dp[j] < dp[tmp[a[j]][k[j]]] + 1) dp[j] = dp[tmp[a[j]][k[j]]] + 1, bck[j] = tmp[a[j]][k[j]]; } for (int j = i; j < to; ++j) { for (int ii = i; ii < j; ++ii) { if (__builtin_popcount(a[ii] & a[j]) == k[j] && dp[j] < dp[ii] + 1) dp[j] = dp[ii] + 1, bck[j] = ii; } } if (to != n) rebuild(to); } int mx = max_element(all(dp)) - dp.begin(); cout << dp[mx] << endl; vector <int> ans; while (mx != -1) { ans.pb(mx + 1); mx = bck[mx]; } reverse(all(ans)); printv(all(ans)); } /* 5 5 3 5 3 5 10 1 20 1 20 4 1 2 3 4 10 0 1 0 */
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...