Submission #506972

#TimeUsernameProblemLanguageResultExecution timeMemory
506972abc864197532Longest beautiful sequence (IZhO17_subsequence)C++17
0 / 100
142 ms205512 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, C = 2000, K = 20; int main () { ios::sync_with_stdio(false); cin.tie(0); 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 <int> dp(n, 0), bck(n, -1); vector <vector <pii>> tmp(1 << K, vector <pii> (K + 1, mp(-1, 0))); auto rebuild = [&](int to) { tmp = vector <vector <pii>> (1 << K, vector <pii> (K + 1, mp(-1, 0))); for (int i = 0; i < to; ++i) { int x = __builtin_popcount(a[i]); tmp[a[i]][x] = max(tmp[a[i]][x], mp(dp[i], 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) { tmp[s][cnt] = max({tmp[s][cnt], tmp[s ^ (1 << i)][cnt - 1], tmp[s ^ (1 << i)][cnt]}); } else { tmp[s][cnt] = max({tmp[s][cnt], tmp[s ^ (1 << i)][cnt], tmp[s ^ (1 << i)][cnt]}); } } } } }; for (int i = 0; i < n; i += C) { int to = min(i + C, n); dp[i] = 1; if (dp[i] < tmp[a[i]][k[i]].X + 1) dp[i] = tmp[a[i]][k[i]].X + 1, bck[i] = tmp[a[i]][k[i]].Y; 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...