제출 #1133928

#제출 시각아이디문제언어결과실행 시간메모리
1133928stdfloatLongest beautiful sequence (IZhO17_subsequence)C++20
100 / 100
3187 ms93364 KiB
#include <bits/stdc++.h>
using namespace std;

using ll = long long;

#define all(v) v.begin(), v.end()

const int N = 1 << 10;

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

	int n;
	cin >> n;

	vector<int> a(n);
	for (auto &i : a)
		cin >> i;

	vector<int> k(n);
	for (auto &i : k)
		cin >> i;

	vector<int> A(n), B(n);
	for (int i = 0; i < n; i++) {
		A[i] = a[i] >> 10;
		B[i] = a[i] & (N - 1);
	}

	vector<int> cnt(N);
	for (int i = 0; i < N; i++)
		cnt[i] = __builtin_popcount(i);

	vector<int> res(n), p(n, -1);
	vector<vector<vector<int>>> dp(N, vector<vector<int>>(N, vector<int>(11, -1)));
	for (int i = 0; i < n; i++) {
		res[i] = 1;
		for (int Aj = 0; Aj < N; Aj++) {
			int x = k[i] - cnt[A[i] & Aj];

			if (0 <= x && x < 11 && ~dp[Aj][B[i]][x] && res[i] < res[dp[Aj][B[i]][x]] + 1) {
				p[i] = dp[Aj][B[i]][x];
				res[i] = res[dp[Aj][B[i]][x]] + 1;
			}
		}

		for (int Bx = 0; Bx < N; Bx++) {
			if (!~dp[A[i]][Bx][cnt[B[i] & Bx]] || res[dp[A[i]][Bx][cnt[B[i] & Bx]]] < res[i])
				dp[A[i]][Bx][cnt[B[i] & Bx]] = i;
		}
	}

	int mx = max_element(all(res)) - res.begin();
	cout << res[mx] << '\n';

	vector<int> v = {mx};
	while (~p[mx]) {
		mx = p[mx];
		v.push_back(mx);
	}

	reverse(all(v));
	for (auto i : v)
		cout << i + 1 << ' ';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...