Submission #340466

#TimeUsernameProblemLanguageResultExecution timeMemory
340466TosicLongest beautiful sequence (IZhO17_subsequence)C++14
0 / 100
4 ms4592 KiB
#include <bits/stdc++.h>
#define maxn 100010
using namespace std;

int n, ans, a[maxn], k[maxn], dp[maxn], prI[maxn], bestL[1<<10][1<<10][11];

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];
	}
	for(int i = 0; i < n; ++i){
		prI[i] = -1;
	}
	for(int i = 0; i < n; ++i){
		int lP = a[i]&((1<<10)-1), rP = a[i]/(1<<10);
		int tmpV = 0, prIdx = -1;
		for(int j = 0; j < (1<<10); ++j){
			// bestL ijnum - max of dp for all states that share num bits with section i of ij
			int bitS = k[i]-__builtin_popcount(j&rP);
			if(bitS <0 or bitS > 10){
				continue;
			}
			if(tmpV < dp[bestL[lP][j][bitS]]){
				tmpV = dp[bestL[lP][j][bitS]];
				prIdx = bestL[lP][j][bitS];
			}
		}
		dp[i] = tmpV+1;
		prI[i] = prIdx;
		for(int j = 0; j < (1<<10); ++j){
			int bitS = __builtin_popcount(j&lP);
			if(dp[i] > dp[bestL[j][rP][bitS]]){
				bestL[j][rP][bitS] = i;
			}
		}
	}
	int frI = 0;
	for(int i = 0; i < n; ++i){
		if(dp[i] > ans){
			ans = dp[i];
			frI = i;
		}
	}
	vector<int> res;
	while(frI > -1){
		res.push_back(frI+1);
		frI = prI[frI];
	}
	cout << res.size() << '\n';
	reverse(res.begin(), res.end());
	for(auto tmp:res){
		cout << tmp << ' ';
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...