Submission #649995

#TimeUsernameProblemLanguageResultExecution timeMemory
649995ymmLongest beautiful sequence (IZhO17_subsequence)C++17
0 / 100
1 ms340 KiB
#include <bits/stdc++.h>
#define Loop(x,l,r) for (ll x = (l); x < (ll)(r); ++x)
#define LoopR(x,l,r) for (ll x = (r)-1; x >= (ll)(l); --x)
typedef long long ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll , ll > pll;
using namespace std;

const int N = 100'010;
int dp[N], pre[N];
int a[N], k[N];
int n;

__attribute__((optimize("O3,unroll-loops"),target("avx")))
int get(int l, int r, int x, int k)
{
	#define MAX(a, b) ((a)>(b)?(a):(b))
	int ans = 0;
	Loop (j,l,r) {
		int tmp = a[j] & x;
		tmp = __builtin_popcount(tmp);
		tmp = -(tmp == k);
		tmp &= (dp[j] & -32768) ^ (j & 32767);
		ans = MAX(ans, tmp);
	}
	return ans;
	#undef MAX
}

void up(int i, int x, int k)
{
	int ans = -1, ansi = -1;
	const int S = 4096;
	for (int l = 0; l < i; l += S) {
		int r = min(l+S, i);
		int tmp = get(l, r, x, k);
		if (tmp > ans) {
			ans = tmp;
			ansi = l;
		}
	}
	if (ansi >= 0) {
		Loop (j,ansi,ansi+S) {
			if (  dp[j]+1 == ans
			    && __builtin_popcount(a[j]&x) == k) {
				ansi = j;
				break;
			}
		}
	}
	pre[i] = ansi;
	dp[i] = ans;
}

int main()
{
	cin.tie(0) -> sync_with_stdio(false);
	cin >> n;
	Loop (i,0,n)
		cin >> a[i];
	Loop (i,0,n)
		cin >> k[i];
	Loop (i,0,n)
		up(i, a[i], k[i]);
	int i = 0;
	Loop (j,0,n) {
		if (dp[j] > dp[i])
			i = j;
	}
	vector<int> vec;
	while (i != -1) {
		vec.push_back(i);
		i = pre[i];
	}
	reverse(vec.begin(), vec.end());
	cout << vec.size() << '\n';
	for (int v : vec)
		cout << v+1 << ' ';
	cout << '\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...