제출 #1007377

#제출 시각아이디문제언어결과실행 시간메모리
1007377vqpahmadLongest beautiful sequence (IZhO17_subsequence)C++17
100 / 100
2063 ms92244 KiB
#include<bits/stdc++.h>
using namespace std;
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
const int MAXN = 1e5 + 15;
int dp[1025][1025][11], n, a[MAXN], b[MAXN];
int pos[1025][1025][11];
int prv[MAXN];

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	cin >> n;
	for (int i = 0; i < n; i++)
		cin >> a[i];
	for (int i = 0; i < n; i++)
		cin >> b[i];

	memset(prv, -1, sizeof(prv));
	pair<int, int> best = {0, 0};
	for (int i = 0; i < n; i++){
		int lhs = a[i] % (1 << 10), rhs = (a[i] >> 10);
		int opt = 0;
		for (int j = 0; j < 1024; j++)
			if (b[i] - __popcount(j&rhs) <= 10 && b[i] - __popcount(j&rhs) >= 0 && ckmax(opt, dp[j][lhs][b[i] - __popcount(j&rhs)]))
				prv[i] = pos[j][lhs][b[i] - __popcount(j&rhs)];

		for (int j = 0; j < 1024; j++)
			if (ckmax(dp[rhs][j][__popcount(j&lhs)], opt + 1))
				pos[rhs][j][__popcount(j&lhs)] = i;

		ckmax(best, {opt + 1, i});
	}
	vector<int> ans;
	for (int i = best.second; i != -1; ans.push_back(i), i = prv[i]);
	reverse(ans.begin(), ans.end());
	cout << ans.size() << endl;
	for (auto it : ans) cout << it + 1 << ' ';
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...