D1. Xor-Subsequence (easy version)

D1. Xor-Subsequence (easy version)

It is the easy version of the problem. The only difference is that in this version ai200.

You are given an array of n integers a0,a1,a2,an1. Bryap wants to find the longest beautiful subsequence in the array.

An array b=[b0,b1,,bm1], where 0b0<b1<<bm1<n, is a subsequence of length m of the array a.

Subsequence b=[b0,b1,,bm1] of length m is called beautiful, if the following condition holds:

  • For any p (0p<m1) holds: abpbp+1<abp+1bp.

Here ab denotes the bitwise XOR of a and b. For example, 24=6 and 31=2.

Bryap is a simple person so he only wants to know the length of the longest such subsequence. Help Bryap and find the answer to his question.

Input

The first line contains a single integer t (1t105)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer n (2n3105) — the length of the array.

The second line of each test case contains n integers a0,a1,...,an1 (0ai200) — the elements of the array.

It is guaranteed that the sum of n over all test cases does not exceed 3105.

Output

For each test case print a single integer — the length of the longest beautiful subsequence.

Example

input

3
2
1 2
5
5 2 4 3 1
10
3 8 8 2 9 1 6 2 8 3

output

2
3
6

Note

In the first test case, we can pick the whole array as a beautiful subsequence because 11<20.

In the second test case, we can pick elements with indexes 1, 2 and 4 (in 0-indexation). For this elements holds: 22<41 and 44<12.

 

解题思路

  如果我们选择了一个合法的序列,(ai1,i1),(ai2,i2),,(aim,im),那么必然有 ai1i2<ai2i1,aim1im<aimim1

  定义状态 f(i) 表示选择 (ai,i) 作为最后一项的所有序列中长度的最大值,根据序列前一个元素是哪个进行状态划分,有状态转移方程f(i)=max0j<iaji<aij{f(j)}+1

  很明显如果直接暴力枚举 j 那么时间复杂度是 O(n2) 的。观察式子 aji<aij,此时已经有 i>j 了,意味着 aji 要变小或 aij 变大。注意到 ai 最大只有 200,在二进制下只有最低 8 位是有效的,即 aj 只对 i 的最低 8 位有影响,同理 ai 只对 j 的最低 8 位有影响。考虑 ij 除了最低 8 位之外的位,即 i28j28,如果 j28<i28,那么无论 aiaj 的值是多少,必然有 aji>aij,所以 j 的下界就是 i28×28,也就是把 i 的最低 8 位都置 0,这样 ji 在除了最低 8 位之外的位都一样。为此 j 的枚举范围应该是 j[i28×28,i1],区间大小最大只有 28

  AC 代码如下,时间复杂度为 O(28n)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 3e5 + 10;

int a[N];
int f[N];

void solve() {
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%d", a + i);
    }
    int ret = 0;
    for (int i = 0; i < n; i++) {
        f[i] = 1;
        for (int j = i >> 8 << 8; j < i; j++) {
            if ((a[j] ^ i) < (a[i] ^ j)) f[i] = max(f[i], f[j] + 1);
        }
        ret = max(ret, f[i]);
    }
    printf("%d\n", ret);
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round #815 (Div. 2) 讲解:https://www.bilibili.com/video/BV1mG4y1a7QS/

  Codeforces Round #815 (Div. 2) Editorial:https://codeforces.com/blog/entry/106136

posted @   onlyblues  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
Web Analytics
点击右上角即可分享
微信分享提示