Submission #340386

#TimeUsernameProblemLanguageResultExecution timeMemory
340386TosicLongest beautiful sequence (IZhO17_subsequence)C++14
23 / 100
103 ms1900 KiB
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;

int n, a[maxn], k[maxn], ans;
pair<int, int> dp[maxn], dp1[300], dp0[maxn];

int main(){
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	cin >> n;
	for(int i = 0; i < n; ++i){
		cin >> a[i];
	}
	for(int i = 0; i < n; ++i){
		cin >> k[i];
	}
	if(n <= 5000){
		for(int i = 0; i < n; ++i){
			dp[i] = {1, -1};
			for(int j = 0; j < i; ++j){
				if(__builtin_popcount(a[i]&a[j]) == k[i]){
					if(dp[j].first+1 > dp[i].first){
						dp[i] = dp[j];
						dp[i].second = j;
						dp[i].first = dp[j].first+1;
					}
				}
			}
		}
		pair<int, int> ans = {1, -1};
		int idx = 0;
		for(int i = 0; i < n; ++i){
			ans = max(ans, dp[i]);
			if(ans == dp[i]){
				idx = i;
			}
		}
		vector<int> res;
		while(true){
			res.push_back(idx+1);
			if(ans.second == -1){
				break;
			}
			idx = ans.second;
			ans = dp[ans.second];
		}
		reverse(res.begin(), res.end());
		cout << res.size() << '\n';
		for(auto tmp:res){
			cout << tmp<<'\n';
		}
		return 0;
	}
	for(int j = 0; j <= (1<<8); ++j){
		dp1[j] = {0, -1};
	}
	for(int i = 0; i < n; ++i){
		dp0[i] = {1, -1};
		for(int j = 0; j <= (1<<8); ++j){
			if(__builtin_popcount(j&a[i]) == k[i]){
				if(dp1[j].first+1 > dp0[i].first){
					dp0[i] = dp1[j];
					dp0[i].first = dp1[j].first+1;
				}
			}
		}
		if(dp0[i].first > dp1[a[i]].first){
			dp1[a[i]] = dp0[i];
			dp1[a[i]].second = i;
		}
	}
	pair<int, int> ans = {1, -1};
		int idx = 0;
		for(int i = 0; i < n; ++i){
			ans = max(ans, dp[i]);
			if(ans == dp[i]){
				idx = i;
			}
		}
		vector<int> res;
		while(true){
			res.push_back(idx+1);
			if(ans.second == -1){
				break;
			}
			idx = ans.second;
			ans = dp[ans.second];
		}
		reverse(res.begin(), res.end());
		cout << res.size() << '\n';
		for(auto tmp:res){
			cout << tmp<<'\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...