Submission #170593

#TimeUsernameProblemLanguageResultExecution timeMemory
170593mcdx9524Longest beautiful sequence (IZhO17_subsequence)C++14
40 / 100
160 ms10768 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N = 1e6 + 7; int dp[N], p[N]; int ndp[N], npos[N]; int main() { ios::sync_with_stdio(0); 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]; dp[i] = 1; p[i] = -1; } if (n > 5000) { for (int i = 0; i < 256; i++) { npos[i] = -1; } for (int i = 0; i < n; i++) { for (int j = 0; j < 256; j++) { if (__builtin_popcount(a[i] & j) == k[i]) { if (ndp[j] >= dp[i]) { dp[i] = ndp[j] + 1; p[i] = npos[j]; } } } if (dp[i] >= ndp[a[i]]) { ndp[a[i]] = dp[i]; npos[a[i]] = i; } } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } vector <int> res; for (int i = 0; i < n; i++) { if (dp[i] == ans) { while (i != -1) { res.push_back(i); i = p[i]; } break; } } reverse(res.begin(), res.end()); cout << ans << '\n'; for (int x : res) { cout << x + 1 << ' '; } return 0; } for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (__builtin_popcount(a[i] & a[j]) == k[i]) { if (dp[j] >= dp[i]) { dp[i] = dp[j] + 1; p[i] = j; } } } } int ans = 0; for (int i = 0; i < n; i++) { ans = max(ans, dp[i]); } vector <int> res; for (int i = 0; i < n; i++) { if (dp[i] == ans) { while (i != -1) { res.push_back(i); i = p[i]; } break; } } reverse(res.begin(), res.end()); cout << ans << '\n'; for (int x : res) { cout << x + 1 << ' '; } return 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...