D2. Xor-Subsequence (hard version)

 

题意:求数组的最长子序列,子序列满足相邻两项 abp⊕bp+1<abp+1⊕bp

分析:二进制字典树dp。(抄jls的,简单分析了一下)

 

#include<bits/stdc++.h>
// cout<<fixed<<setprecision(8);
//#pragma GCC optimize(2)
#define ms(x,n) memset(x,n,sizeof x)
#define endl '\n'
#define pii pair<int,int> 
#define M(a,b) make_pair(a,b)
#define fi first
#define se second
#define pb push_back
#define __builtin_popcount popcnt1//getcnt 
#define db1(x) cerr<<#x<<"="<<(x)<<" "
#define db2(x) cerr<<#x<<"="<<(x)<<"\n"
//#define int long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
ll _gcd(ll a, ll b) { return b > 0 ? _gcd(b, a % b) : a; }
int popcnt1(ll x) { int ans = 0;for (int j = 0;j <= 60;j++)if (x & (1ll << j)) ans++;return ans; }
template <typename T> inline void read(T& t) { int f = 0, c = getchar(); t = 0;while (!isdigit(c)) f |= c == '-', c = getchar();while (isdigit(c)) t = t * 10 + c - 48, c = getchar();if (f) t = -t; }
template <typename T> void print(T x) { if (x < 0) x = -x, putchar('-');if (x > 9) print(x / 10);putchar(x % 10 + 48); }
const double pi = acos(-1.0);
const double eps = 1e-7;//有时候精度可能不够
const int inf = 0x3f3f3f3f;
const ll Inf = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
ll power(ll n, ll x) { ll res = 1;ll q = n % mod;while (x) { if (x & 1) res = (res * q) % mod;q = q * q % mod;x /= 2; }return res; }
const int N = 3e5*31+10;
int n, m, t, k;
int a[N];
int trie[N][2], f[N][2];
int dp[N / 31];
int cnt = 0;
int newnode() {
    cnt++;
    trie[cnt][0] = trie[cnt][1] = 0;
    f[cnt][0] = f[cnt][1] = 0;
    return cnt;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> t;
    while (t--) {
        cin >> n;
        cnt = 0;
        for (int i = 0;i < n;i++) {
            dp[i] = 0;
        }
        newnode();
        for (int i = 0;i < n;i++) {
            cin >> a[i];
            int x = a[i] ^ i;
            int p = 1;
            for (int j = 30;j >= 0;j--) {
                //db2(p);
                int v = x >> j & 1;
                dp[i] = max(dp[i], f[trie[p][v ^ 1]][i >> j & 1]);
                p = trie[p][v];
                if (!p) break;
            }
            dp[i]++;
            p = 1;
            for (int j = 30;j >= 0;j--) {
                int v = x >> j & 1;
                if (!trie[p][v]) {
                    trie[p][v] = newnode();
                }
                p = trie[p][v];
                f[p][a[i] >> j & 1] = max(f[p][a[i] >> j & 1], dp[i]);
            }
        }
        int ans = 0;
        for (int i = 0;i < n;i++) {
            //db1(i), db2(dp[i]);
            ans = max(ans, dp[i]);
        }
        cout << ans << endl;
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值