Submission #531228

#TimeUsernameProblemLanguageResultExecution timeMemory
531228clamsLongest beautiful sequence (IZhO17_subsequence)C++17
40 / 100
288 ms2200 KiB
/**
 * @authors bubu
 * @date    2022-02-27 21:34:58
 * 
 * challenge: none
 */
#include <bits/stdc++.h>
using namespace std;
#define cnt1 __builtin_popcount

int n;
vector<int> a, k;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    a.resize(n);
    k.resize(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++) {
        cin >> k[i];
    }

    if (n <= 5000) { 
        // ez sub 1 and sub 2 - basic O(N^2) lis
        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 (cnt1(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;
    }
 
    if (*max_element(a.begin(), a.end()) < (1 << 8)) {
        // 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
        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 (cnt1(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 😎 = posmx;
        while (😎 >= 0) {
            ans.push_back(😎);
            😎 = p[😎];
        }

        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...