제출 #1100011

#제출 시각아이디문제언어결과실행 시간메모리
1100011MuhammetLongest beautiful sequence (IZhO17_subsequence)C++17
23 / 100
6051 ms1248 KiB
#include <bits/stdc++.h>

using namespace std;

int n, m[1<<20];

vector <int> a, k;

int bitcount(int x){
	int ans = 0;
	while(x){
		ans += (x%2);
		x /= 2;
	}
	return ans;
}

int f(int x, int k2, int ind){
	int ans = 0;
	for(int i = 0; i <= (1<<8); i++){
		// cout << x << ' ' << i << ' ' << (x&i) << ' ' << bitcount(x&i) << '\n';
		if(bitcount(x&i) == k2){
			// cout << y << ' ' << i << ' ' << m[i] << '\n';
			ans = max(ans,m[i]);
		}
	}
	return ans+1;
}

int main(){
	cin >> n;
	a.resize(n+1);
	k.resize(n+1);
	int mx = 0;
	for(int i = 1; i <= n; i++){
		cin >> a[i];
		mx = max(mx,a[i]);
	}
	for(int i = 1; i <= n; i++){
		cin >> k[i];
	}
	if(mx > (1<<8) or n <= 5000){
		vector <int> dp(n+1,1);
		int mx = 0, ind = 1;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j < i; j++){
				if(bitcount(a[i] & a[j]) == k[i]){
					dp[i] = max(dp[i],dp[j] + 1);
				}
			}
			if(mx < dp[i]) ind = i;
			mx = max(mx,dp[i]);
		}
		cout << mx << '\n';
		vector <int> v;
		while(1){
			v.push_back(ind);
			if(dp[ind] == 1) break;
			for(int i = ind; i >= 1; i--){
				if(bitcount(a[ind] & a[i]) == k[ind] and dp[i] == dp[ind]-1){
					ind = i;
					break;
				}
			}
		}
		reverse(v.begin(), v.end());
		for(auto i : v){
			cout << i << ' ';
		}
		return 0;
	}
	int ans = 1, ind = 1;
	for(int i = 1; i <= n; i++){
		// cout << i << ' ' << k[i] << '\n';
		m[a[i]] = max(f(a[i],k[i],i),m[a[i]]);
		if(m[a[i]] > ans){
			ind = i;
		}
		ans = max(ans,m[a[i]]);
		// cout << i << ' ' << m[a[i]] << '\n';
	}
	cout << ans << '\n';
	vector <int> v;
	while(1){
		v.push_back(ind);
		if(m[a[ind]] == 1 or ind == 1) break;
		for(int i = ind-1; i >= 1; i--){
			if(bitcount(a[ind] & a[i]) == k[ind] and m[a[i]] == m[a[ind]]-1){
				ind = i;
				break;
			}
		}
	}
	reverse(v.begin(), v.end());
	for(auto i : v){
		cout << i << ' ';
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...