D1. Xor-Subsequence (easy version) DP

Problem - 1720D1 - Codeforces

D1. Xor-Subsequence (easy version)

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

It is the easy version of the problem. The only difference is that in this version ai≤200ai≤200.

You are given an array of nn integers a0,a1,a2,…an−1a0,a1,a2,…an−1. Bryap wants to find the longest beautiful subsequence in the array.

An array b=[b0,b1,…,bm−1]b=[b0,b1,…,bm−1], where 0≤b0<b1<…<bm−1<n0≤b0<b1<…<bm−1<n, is a subsequence of length mm of the array aa.

Subsequence b=[b0,b1,…,bm−1]b=[b0,b1,…,bm−1] of length mm is called beautiful, if the following condition holds:

  • For any pp (0≤p<m−10≤p<m−1) holds: abp⊕bp+1<abp+1⊕bpabp⊕bp+1<abp+1⊕bp.

Here a⊕ba⊕b denotes the bitwise XOR of aa and bb. For example, 2⊕4=62⊕4=6 and 3⊕1=23⊕1=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 tt (1≤t≤1051≤t≤105)  — the number of test cases. The description of the test cases follows.

The first line of each test case contains a single integer nn (2≤n≤3⋅1052≤n≤3⋅105) — the length of the array.

The second line of each test case contains nn integers a0,a1,...,an−1a0,a1,...,an−1 (0≤ai≤2000≤ai≤200) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 3⋅1053⋅105.

Output

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

Example

input

Copy

 

3

2

1 2

5

5 2 4 3 1

10

3 8 8 2 9 1 6 2 8 3

output

Copy

2
3
6

Note

In the first test case, we can pick the whole array as a beautiful subsequence because 1⊕1<2⊕01⊕1<2⊕0.

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

---------------------------------------------------------------------------------------------------------------------------------

毫无规律可言的题目考虑DP,很显然的一个转移思想就是  dp[i]=dp[j]+1 其中j<i,接下来考虑优化。

根据异或性质   设x>=y   有x-y<= x^y <= x+y 

根据题目条件进行转化

 设 “大”为 bp+1 也就是较大下标,"小"为较小下标

等式成立即极大值小于极小值

a[小] ^ 大< 小^ a[大] 

考虑“大”"小",极大时(>200)

大-a[小]  < 小+a[大]

大-小 < a[小]+a[大]   

即较大下标与较小下标不超过400的差距,暴力枚举即可

# include<bits/stdc++.h>
using namespace std;
typedef long long int ll;

int a[300000+10];
int dp[300000+10];
int main()
{

   int t;
   cin>>t;
   while(t--)
   {
       int n;
       cin>>n;
       for(int i=0;i<n;i++)
       {
           cin>>a[i];
           dp[i]=1;
       }
       int ans=1;

       for(int i=0;i<n;i++)
       {
           for(int j=max(0,i-400);j<i;j++)
           {
               if((a[j]^i)<(a[i]^j))
               dp[i]=max(dp[i],dp[j]+1);
               ans=max(ans,dp[i]);
           }
       }
       cout<<ans<<endl;
   }


    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值