Submission #884503

#TimeUsernameProblemLanguageResultExecution timeMemory
884503tsumondaiLongest beautiful sequence (IZhO17_subsequence)C++14
100 / 100
1907 ms85912 KiB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define foru(i, l, r) for(int i = l; i <= r; i++)
#define ford(i, r, l) for(int i = r; i >= l; i--)
#define __TIME  (1.0 * clock() / CLOCKS_PER_SEC)

typedef pair<int, int> ii;
typedef pair<ii, int> iii;
typedef pair<ii, ii> iiii;

const int N = 1e5 + 5, B = 1<<10;

const int oo = 1e9, mod = 1e9 + 7;
int a[N], b[N], bit[1 << 11], f[11][1 << 10][1 << 10], k[N], dp[N];

void process() {
    cin.tie(0) -> sync_with_stdio(0);
    int n; cin >> n;
    for(int mask = 0; mask < (1 << 10); mask++) bit[mask] = __builtin_popcount(mask);
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
        b[i] = (a[i] >> 10);
        a[i] ^= (b[i] << 10);
    }
    int trace = 0;
    for(int i = 1; i <= n; i++) {
        cin >> k[i];
        dp[i] = 1;
        for(int j = 0; j < (1 << 10); j++) {
            int x = bit[a[i] & j];
            int y = k[i] - x;
            if (y < 0 || y > 10) continue;
            dp[i] = max(dp[i], f[y][j][b[i]] + 1);
        }
        for(int j = 0; j < (1 << 10); j++) {
            int y = bit[b[i] & j];
            f[y][a[i]][j] = max(f[y][a[i]][j], dp[i]);
        }
        if (dp[trace] < dp[i]) trace = i;
    }
    vector<int> ans;
    ans.push_back(trace);
    for(int i = trace - 1; i >= 1; i--) {
        if (bit[a[i] & a[trace]] + bit[b[i] & b[trace]] == k[trace] && dp[i] + 1 == dp[trace]) {
            ans.push_back(i);
            trace = i;
        }
    }
    cout << ans.size() << endl;
    reverse(ans.begin(), ans.end());
    for(int &j : ans) cout << j << " ";
}

signed main() {
	cin.tie(0)->sync_with_stdio(false);
	//freopen(".inp", "r", stdin);
	//freopen(".out", "w", stdout);
	process();
	cerr << "Time elapsed: " << __TIME << " s.\n";
	return 0;
}

// dont stop
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...