CF1720D2(字典树解决异或运算+dp)

 

思路:

我们可以搞出一个dp方程, 令dp_i为以第i个数结尾的最长的子序列长度:

dp_i=\max_{a_j\bigoplus i<a_i \bigoplus j\wedge j<i }\{dp_j\}+1.考虑优化这个方程.看到这个位运算,我们比较烦的一点就是不等号,我们尝试变成等号,a_j ^ i < a_i ^ j,此时我们在二进制的情况下观察这个不等式,发现从最高位开始,将会出现第一个位置是a_j ^ i != a_i ^ j的,由于是小于号,所以在这个位上,a_j ^ i是0,a_i ^ j是1.此时我们画个真值表看一下a_i,a_j,i,j的取值.

a_ia_jija_i ^ ja_j ^ i
100010
000110
111010
011110

观察到j = a_i ^ 1, 此时我们在第k位如果知道了a_i,那么j的值唯一确定(j < i).

在第k位之前呢,a_j ^ i = a_i ^ j,此时移个项,a_j ^ j = a_i ^ i.

此时我们可以枚举不同的位置是哪个,也就是上文所说的第k位,然后用满足条件的dp值更新.这个我们可以用字典树解决.tr[p][0/1]用于存储a_i ^ i的值,f[p][0/1]表示在这个节点上i的取值为0/1时的最长子序列长度.首先,我们解释一下为什么有f[p][0/1]这个数组,在dp转移的时候,我们要枚举每一位上a_i ^ i与a_j ^ j不同的情况,此时在这一位上a_i ^ i ^ 1 = a_j ^ j,我们直接查询这个节点上的最大值是不行的,因为我们上面列了真值表,还需要满足j = a_i ^ 1,此时我们还需要加上限制j的取值.具体细节可以看代码.

代码:

#include <bits/stdc++.h>
// #define int long long
#define IOS ios::sync_with_stdio(false), cin.tie(0) 
#define ll long long 
#define double long double
#define ull unsigned long long 
#define PII pair<int, int> 
#define PDI pair<double, int> 
#define PDD pair<double, double> 
#define debug(a) cout << #a << " = " << a << endl 
#define point(n) cout << fixed << setprecision(n)
#define all(x) (x).begin(), (x).end() 
#define mem(x, y) memset((x), (y), sizeof(x)) 
#define lbt(x) (x & (-x)) 
#define SZ(x) ((x).size()) 
#define inf 0x3f3f3f3f 
#define INF 0x3f3f3f3f3f3f3f3f
namespace nqio{const unsigned R = 4e5, W = 4e5; char *a, *b, i[R], o[W], *c = o, *d = o + W, h[40], *p = h, y; bool s; struct q{void r(char &x){x = a == b && (b = (a = i) + fread(i, 1, R, stdin), a == b) ? -1 : *a++;} void f(){fwrite(o, 1, c - o, stdout); c = o;} ~q(){f();}void w(char x){*c = x;if (++c == d) f();} q &operator >>(char &x){do r(x);while (x <= 32); return *this;} q &operator >>(char *x){do r(*x); while (*x <= 32); while (*x > 32) r(*++x); *x = 0; return *this;} template<typename t> q&operator>>(t &x){for (r(y),s = 0; !isdigit(y); r(y)) s |= y == 45;if (s) for (x = 0; isdigit(y); r(y)) x = x * 10 - (y ^ 48); else for (x = 0; isdigit(y); r(y)) x = x * 10 + (y ^ 48); return *this;} q &operator <<(char x){w(x);return *this;}q &operator<< (char *x){while (*x) w(*x++); return *this;}q &operator <<(const char *x){while (*x) w(*x++); return *this;}template<typename t> q &operator<< (t x) {if (!x) w(48); else if (x < 0) for (w(45); x; x /= 10) *p++ = 48 | -(x % 10); else for (; x; x /= 10) *p++ = 48 | x % 10; while (p != h) w(*--p);return *this;}}qio; }using nqio::qio;
using namespace std;
const int N = 3e5 + 10;
int n, a[N], tr[N * 32][2], f[N * 32][2], dp[N], tot;
void insert(int x, int id) {
	int p = 0;
	for (int i = 30; i >= 0; --i) {
		int bit = x >> i & 1;
		if (!tr[p][bit]) tr[p][bit] = ++tot;
		p = tr[p][bit];
		f[p][id >> i & 1] = max(f[p][id >> i & 1], dp[id]);
	}
}
int ask(int x, int k) {
	int p = 0, ans = 1;
	for (int i = 30; i >= 0; --i) {
		int bit = x >> i & 1, kk = k >> i & 1;
		ans = max(ans, f[tr[p][bit ^ 1]][kk ^ 1] + 1);
		if (!tr[p][bit]) break;
		p = tr[p][bit];
	}
	return ans;
}
void solve() {
	for (int i = 0; i <= tot; ++i) f[i][0] = f[i][1] = tr[i][0] = tr[i][1] = 0;
	tot = 0;
	qio >> n;
	for (int i = 0; i < n; ++i) dp[i] = 1;
	for (int i = 0; i < n; ++i) {
		qio >> a[i];
		dp[i] = ask(a[i] ^ i, a[i]);
		insert(a[i] ^ i, i);
	}
	qio << *max_element(dp, dp + n) << "\n";
}
signed main() {
	int T;
	qio >> T;
	while (T--) solve();
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值