제출 #333886

#제출 시각아이디문제언어결과실행 시간메모리
333886amunduzbaevLongest beautiful sequence (IZhO17_subsequence)C++14
40 / 100
122 ms4588 KiB
#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long 
#define ld long double 
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 505;
 
 
void solve(){
	fastios
	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];
	}
	
	vector<int>dp(n, 0);
	vector<int>par(n, -1);
	vector<int>used(300);
	vector<int>id(300, -1);
	
	for(int i=0;i<n;i++){
		dp[i] = 1;
		
		if(n <= 5000){
			for(int j=0;j<i;j++){
				if(__builtin_popcount(a[i] & a[j]) == k[i] && dp[i] < dp[j] + 1){
					dp[i] = dp[j] + 1;
					par[i] = j;
				}
			}
		}
		
		else{
			for(int j=0;j<256;j++){
				if(used[j] && __builtin_popcount(a[i] & j) == k[i] && dp[i] < used[j] + 1){
					dp[i] = used[j] + 1;
					par[i] = id[j]-1;
				}
			}
			if(dp[i] > used[a[i]]){
				used[a[i]] = dp[i];
				id[a[i]] = i+1;
			}
		}
	}
	int ans = 0, s = 0;
	for(int i=0;i<n;i++){
		if(dp[i] > ans){
			ans = dp[i];
			s = i;
		}
	}
	
	vector<int>xx;
	
	while(s != -1){
		xx.pb(s+1);
		s = par[s];
	}
	sort(all(xx));
	cout<<sz(xx)<<"\n";
	for(int i = 0; i < sz(xx); i++){
		cout<<xx[i]<<" ";
	}cout<<"\n";
	
	return;
}

/*

4
1 2 3 4
10 0 1 0 

 * */
 
int main(){
	fastios
	int t = 1;
	//cin>>t;
	while(t--) solve();
	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...