Submission #531194

#TimeUsernameProblemLanguageResultExecution timeMemory
531194clamsLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
255 ms2132 KiB
/** * @authors bubu * @date 2022-02-27 21:34:58 * * challenge: none */ #include <bits/stdc++.h> using namespace std; 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]; } // ez sub 1 and sub 2 - basic O(N^2) lis if (n <= 5000) { vector<int> d(n, 1); // len of the longest beautiful sequence ends at i vector<int> p(n, -1); for (int i = 0; i < n; i++) { for (int j = 0; j < i; j++) { if (__builtin_popcount(a[i] & a[j]) == k[i]) { // can we transform this if (d[j] + 1 > d[i]) { d[i] = d[j] + 1; p[i] = j; } } } } vector<int> ans; int it = max_element(d.begin(), d.end()) - d.begin(); while (it >= 0) { ans.push_back(it); it = p[it]; } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for (int i : ans) cout << i + 1 << ' '; cout << '\n'; return 0; } // use trie? // sub 3 ? we can use a[i]^2 // let's try the idea // nope, wouldn't work // must use something that is algorithmically hard or something i haven't know // sub 3: this is interesting, at least for me, lis, modified to work with small values // idea, since a[i] < 256, we keep a map where the key is a[i], value is {len of lis end here, position} // maybe don't need map, an array of 256 elements is better, no, not really, when looping through the map, it's worse if (*max_element(a.begin(), a.end()) < (1 << 8)) { map<int, pair<int, int>> v; vector<int> p(n, -1); int mx = 0; int posmx = -1; for (int i = 0; i < n; i++) { int curmx = 1; for (auto [x, y] : v) { if (__builtin_popcount(a[i] & x) == k[i]) { if (y.first + 1 > curmx) { curmx = y.first + 1; p[i] = y.second; } } } if (curmx > v[a[i]].first) { v[a[i]] = {curmx, i}; } if (curmx > mx) { mx = curmx; posmx = i; } } vector<int> ans; int it = posmx; while (it >= 0) { ans.push_back(it); it = p[it]; } cout << ans.size() << '\n'; reverse(ans.begin(), ans.end()); for (int i : ans) cout << i + 1 << ' '; cout << '\n'; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...