Submission #1124394

#TimeUsernameProblemLanguageResultExecution timeMemory
1124394votranngocvyLongest beautiful sequence (IZhO17_subsequence)C++20
23 / 100
238 ms204660 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int a[N],k[N],n; namespace sub2 { int dp[N],trace[N]; void solve() { int ans = 0,cur = 0; for (int i = 1; i <= n; i++) { dp[i] = 1; for (int j = 1; j < i; j++) if (__builtin_popcount(a[i] & a[j]) == k[i] && dp[i] < dp[j] + 1) { dp[i] = dp[j] + 1; trace[i] = j; } if (dp[i] > ans) ans = dp[i],cur = i; } vector<int>vec; for (int i = cur; i > 0; i = trace[i]) vec.push_back(i); reverse(vec.begin(),vec.end()); cout << ans << "\n"; for (auto x: vec) cout << x << " "; cout << "\n"; } } namespace sub3 { int dp[N][260],trace[N][260]; bool check_condition() { for (int i = 1; i <= n; i++) if (a[i] >= (1 << 8)) return false; return true; } void solve() { for (int i = 1; i <= n; i++) { dp[i][a[i]] = 1; for (int mask = 0; mask < (1 << 8); mask++) { if (dp[i - 1][mask] > dp[i][mask]) { dp[i][mask] = dp[i - 1][mask]; trace[i][mask] = mask; } if (__builtin_popcount(mask & a[i]) == k[i] && dp[i - 1][mask] + 1 > dp[i][a[i]]) { dp[i][a[i]] = dp[i - 1][mask] + 1; trace[i][a[i]] = mask; } } } int ans = 0,cur = 0; for (int mask = 0; mask < (1 << 8); mask++) if (dp[n][mask] > ans) ans = dp[n][mask],cur = mask; vector<int>vec; int i = n,j = ans; while (true) { if (dp[i][cur] == j && cur == a[i]) { vec.push_back(i); j--; } if (dp[i][cur] == 1) break; cur = trace[i][cur]; i--; } reverse(vec.begin(),vec.end()); cout << ans << "\n"; for (auto x: vec) cout << x << " "; cout << "\n"; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) cin >> k[i]; if (n <= 5000) sub2::solve(); else if (sub3::check_condition()) sub3::solve(); //else sub4::solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...