제출 #515634

#제출 시각아이디문제언어결과실행 시간메모리
515634MazaalaiLongest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
2537 ms97348 KiB
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
#define LINE "--------------------\n"
#define ALL(x) x.begin(),x.end()
using namespace std;
using PII = pair <int, int>;
const int N = 1e5+5;
const int M = (1<<10);
int n;
int nums[N], bits[N], res[N], par[N];
PII dp[M][M][11]; // first value, second parent
int match[M][M];
int bitCnt(int a) {
	return bitset <20>(a).count();
}
signed main() {
	// freopen("in.txt", "r", stdin);
	// freopen("out.txt", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin >> n;
	int ans = 1;
	for (int i = 1; i <= n; i++) cin >> nums[i];
	for (int i = 1; i <= n; i++) cin >> bits[i];

	for (int i = 0; i < M; i++)
	for (int j = i; j < M; j++) { // 2^20 * 20
		match[i][j] = match[j][i] = bitCnt(i&j);
	}
	// cout << match[3][3] << '\n';
	memset(dp, 0, sizeof(dp));
	for (int i = 1; i <= n; i++) { // n*2^20
		PII cur = {0, 0};
		int lhs = nums[i]>>10, rhs = nums[i]%(1<<10);
		for (int mask = 0; mask < M; mask++) { // 2^10
			int matched = match[lhs][mask], tar = bits[i] - matched;
			if (matched > bits[i] || (tar > 10)) continue;
			if (dp[mask][rhs][tar].ff > cur.ff) cur = dp[mask][rhs][tar];
		}
		res[i] = cur.ff+1;
		par[i] = cur.ss;

		for (int mask = 0; mask < M; mask++) { // 2^20
			int matched = match[rhs][mask];
			if (dp[lhs][mask][matched].ff < res[i]) {
				dp[lhs][mask][matched] = {res[i], i};
			}
		}
		if (res[i] > res[ans]) ans = i;
		// cout << res[i] << "," << par[i] << " \n"[i==n];
	}
	vector <int> tmp;
	while(ans != 0) {
		tmp.pb(ans);
		ans = par[ans];
	}
	cout << tmp.size() << '\n';
	while(!tmp.empty()) {
		cout << tmp.back() << " ";
		tmp.pop_back();
	}
	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...