Submission #507719

#TimeUsernameProblemLanguageResultExecution timeMemory
507719abc864197532Longest beautiful sequence (IZhO17_subsequence)C++17
0 / 100
229 ms262148 KiB
#include <bits/stdc++.h>
using namespace std;
#define lli long long int
#define mp make_pair
#define eb emplace_back
#define pb push_back
#define X first
#define Y second
#define pii pair<int, int>
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void abc() {cout << endl;}
template <typename T, typename ...U> void abc(T i, U ...j) {
	cout << i << ' ', abc(j...);
}
template <typename T> void printv(T l, T r) {
	for (; l != r; ++l)
		cout << *l << " \n"[l + 1 == r];
}
#define test(x...) abc("[" + string(#x) + "]", x);
const int N = 200000;

int bucket[1 << 10][11][1 << 10], popcnt[1 << 10];

int main () {
	ios::sync_with_stdio(false);
	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];
	for (int s = 1; s < 1 << 10; ++s)
		popcnt[s] = popcnt[s ^ (s & (-s))] + 1;
	vector <int> dp(n, 1), bck(n, -1);
	auto upd = [&](int &i, int j) {
		if (i == -1)
			i = j;
		else if (dp[i] < dp[j])
			i = j;
	};
	for (int s = 0; s < 1 << 10; ++s) for (int i = 0; i < 10; ++i) for (int t = 0; t < 1 << 10; ++t)
		bucket[s][i][t] = -1;
	for (int i = 0; i < n; ++i) {
		for (int s = 0; s < 1 << 10; ++s) {
			int j = k[i] - popcnt[s & (a[i] >> 10)];
			if (j < 0 || j > 10)
				continue;
			int x = bucket[a[i] & 1023][j][s];
			if (x != -1 && dp[i] < dp[x] + 1)
				dp[i] = dp[x] + 1, bck[i] = x;
		}
		for (int s = 0; s < 1 << 10; ++s) {
			upd(bucket[s][popcnt[s & a[i]]][a[i] >> 10], i);
		}
	}
	int mx = max_element(all(dp)) - dp.begin();
	cout << dp[mx] << endl;
	vector <int> ans;
	while (mx != -1) {
		ans.pb(mx + 1);
		mx = bck[mx];
	}
	reverse(all(ans));
	printv(all(ans));
}

/*
5
5 3 5 3 5
10 1 20 1 20

4
1 2 3 4
10 0 1 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...