Submission #1269071

#TimeUsernameProblemLanguageResultExecution timeMemory
1269071lmduc270410Longest beautiful sequence (IZhO17_subsequence)C++20
40 / 100
6093 ms4636 KiB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define ldb long double
#define pii pair<int, int>
#define bend(v) v.begin(), v.end()
const int N = 1e5 + 5, M = 2e6 + 5;
const int mod = 1e9 + 7;
const int base = 311;
const int inf = INT_MAX;
const ll INF = LLONG_MAX;
const int MOD = 1e9 + 7;
const int BUF_SZ = 1 << 15;

inline namespace Input {
char buf[BUF_SZ];
int pos;
int len;
char next_char() {
	if (pos == len) {
		pos = 0;
		len = (int)fread(buf, 1, BUF_SZ, stdin);
		if (!len) { return EOF; }
	}
	return buf[pos++];
}

int read_int() {
	int x;
	char ch;
	int sgn = 1;
	while (!isdigit(ch = next_char())) {
		if (ch == '-') { sgn *= -1; }
	}
	x = ch - '0';
	while (isdigit(ch = next_char())) { x = x * 10 + (ch - '0'); }
	return x * sgn;
}
}  // namespace Input
inline namespace Output {
char buf[BUF_SZ];
int pos;

void flush_out() {
	fwrite(buf, 1, pos, stdout);
	pos = 0;
}

void write_char(char c) {
	if (pos == BUF_SZ) { flush_out(); }
	buf[pos++] = c;
}

void write_int(int x) {
	static char num_buf[100];
	if (x < 0) {
		write_char('-');
		x *= -1;
	}
	int len = 0;
	for (; x >= 10; x /= 10) { num_buf[len++] = (char)('0' + (x % 10)); }
	write_char((char)('0' + x));
	while (len) { write_char(num_buf[--len]); }
	// write_char(' ');
}

// auto-flush output when program exits
void init_output() { assert(atexit(flush_out) == 0); }
}  // namespace Output

int n, a[N], k[N];
short pcnt[M];
pii dp[N];
int d[25][N], c[25], idk[N];

void solve(){
	
	cin>>n;
	for(int i = 1; i <= n; i++) a[i] = read_int();
	for(int i = 1; i <= n; i++) k[i] = read_int();
	for(int i = 1; i <= (1 << 20); i++) pcnt[i] = __builtin_popcount(i);

	pii ans = {0, 0};
	for(int i = 1; i <= n; i++){
		dp[i] = {1, i};
		int cb = pcnt[a[i]];
		// cout<<i<<" "<<a[i]<<" "<<cb<<" "<<k[i]<<'\n';
		if(cb < k[i]){
			ans = max(ans, {1, i});
			d[cb][++c[cb]] = i;
			continue;
		}
		for(int v = k[i]; v <= 20; v++){
			for(int t = 1; t <= c[v]; t++){
				int j = d[v][t];
				if(pcnt[(a[i] & a[j])] == k[i]){
					dp[i] = max(dp[i], {dp[j].fi + 1, j});
				}
			}
		}
		ans = max(ans, {dp[i].fi, i});
		d[cb][++c[cb]] = i;
	}

	// for(int i = 1; i <= n; i++) cout<<"!!! "<<dp[i].fi<<" "<<dp[i].se<<'\n';

	write_int(ans.fi);
	write_char('\n');
	int m = 0, cur = ans.se;
	while(dp[cur].se != cur){
		idk[++m] = cur;
		cur = dp[cur].se;
	}
	idk[++m] = cur;
	reverse(idk + 1, idk + m + 1);
	for(int i = 1; i <= m; i++) write_int(idk[i]), write_char(' ');
}

signed main(){
	
	// freopen(".inp", "r", stdin);
	// freopen(".out", "w", stdout);

	// ios_base::sync_with_stdio(0);
	// cin.tie(0); cout.tie(0);

	init_output();

	int tc = 1;
	// cin>>tc;
	while(tc--) solve();
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...