제출 #486685

#제출 시각아이디문제언어결과실행 시간메모리
486685ez_ioiLongest beautiful sequence (IZhO17_subsequence)C++17
7 / 100
245 ms94344 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int mxN = 1e6 + 5, mod = 1e9 + 7, LOG = 20;

int n, a[mxN], k[mxN], p[mxN], dp[mxN];

pair <int, int> st[2005][2005][11];

int mask1[mxN], mask2[mxN];

int main() {
	ios :: sync_with_stdio(false), cin.tie(nullptr);
	cin >> n;
	vector <int> f, s;
	for (int i = 1; i <= n; ++i) {
		cin >> a[i];
		for (int j = 0; j < 10; ++j) {
			if ((a[i] >> j) & 1) mask1[i] += (1 << j);
		}
		for (int j = 10; j < 20; ++j) {
			if ((a[i] >> j) & 1) mask2[i] += (1 << (j - 10));
		}
		f.push_back(mask1[i]); s.push_back(mask2[i]);
	}
	for (int i = 1; i <= n; ++i) cin >> k[i];
	sort(f.begin(), f.end());
	f.erase(unique(f.begin(), f.end()), f.end());
	sort(s.begin(), s.end());
	s.erase(unique(s.begin(), s.end()), s.end());
	pair <int, int> ans = {0, 0};
	for (int i = 1; i <= n; ++i) {
		dp[i] = 1;
		{ // get
			for (auto mask : f) {
				int x = __builtin_popcount(mask1[i] & mask);
				if (x > k[i]) continue;
				pair <int, int> y = st[mask][mask2[i]][k[i] - x];
				if (y.first + 1 > dp[i]) {
					p[i] = y.second;
					dp[i] = y.first + 1;
				}
			}
		}
		{ // upd
			for (auto mask : s) {
				int x = __builtin_popcount(mask & mask2[i]);
				st[mask1[i]][mask][x] = max(st[mask1[i]][mask][x], {dp[i], i});
			}
		}
		ans = max(ans, {dp[i], i});
	}
	int now = ans.second;
	vector <int> seq;
	while (now > 0) {
		seq.push_back(now);
		now = p[now];
	}
	reverse(seq.begin(), seq.end());
	cout << (int)seq.size() << '\n';
	for (auto x : seq) cout << x << ' ';
	return 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...