B. Unmergen -布尔DP

B. Unmerge

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Let aa and bb be two arrays of lengths nn and mm, respectively, with no elements in common. We can define a new array merge(a,b)merge(a,b) of length n+mn+m recursively as follows:

  • If one of the arrays is empty, the result is the other array. That is, merge(∅,b)=bmerge(∅,b)=b and merge(a,∅)=amerge(a,∅)=a. In particular, merge(∅,∅)=∅merge(∅,∅)=∅.
  • If both arrays are non-empty, and a1<b1a1<b1, then merge(a,b)=[a1]+merge([a2,…,an],b)merge(a,b)=[a1]+merge([a2,…,an],b). That is, we delete the first element a1a1 of aa, merge the remaining arrays, then add a1a1 to the beginning of the result.
  • If both arrays are non-empty, and a1>b1a1>b1, then merge(a,b)=[b1]+merge(a,[b2,…,bm])merge(a,b)=[b1]+merge(a,[b2,…,bm]). That is, we delete the first element b1b1 of bb, merge the remaining arrays, then add b1b1 to the beginning of the result.

This algorithm has the nice property that if aa and bb are sorted, then merge(a,b)merge(a,b) will also be sorted. For example, it is used as a subroutine in merge-sort. For this problem, however, we will consider the same procedure acting on non-sorted arrays as well. For example, if a=[3,1]a=[3,1] and b=[2,4]b=[2,4], then merge(a,b)=[2,3,1,4]merge(a,b)=[2,3,1,4].

A permutation is an array consisting of nn distinct integers from 11 to nn in arbitrary order. For example, [2,3,1,5,4][2,3,1,5,4] is a permutation, but [1,2,2][1,2,2] is not a permutation (22 appears twice in the array) and [1,3,4][1,3,4] is also not a permutation (n=3n=3 but there is 44 in the array).

There is a permutation pp of length 2n2n. Determine if there exist two arrays aa and bb, each of length nn and with no elements in common, so that p=merge(a,b)p=merge(a,b).

Input

The first line contains a single integer tt (1≤t≤10001≤t≤1000)  — the number of test cases. Next 2t2t lines contain descriptions of test cases.

The first line of each test case contains a single integer nn (1≤n≤20001≤n≤2000).

The second line of each test case contains 2n2n integers p1,…,p2np1,…,p2n (1≤pi≤2n1≤pi≤2n). It is guaranteed that pp is a permutation.

It is guaranteed that the sum of nn across all test cases does not exceed 20002000.

Output

For each test case, output "YES" if there exist arrays aa, bb, each of length nn and with no common elements, so that p=merge(a,b)p=merge(a,b). Otherwise, output "NO".

Example

input

Copy

6
2
2 3 1 4
2
3 1 2 4
4
3 2 6 1 5 7 8 4
3
1 2 3 4 5 6
4
6 1 3 7 4 5 8 2
6
4 3 2 5 1 11 9 12 8 6 10 7

output

Copy

YES
NO
YES
YES
NO
NO

Note

In the first test case, [2,3,1,4]=merge([3,1],[2,4])[2,3,1,4]=merge([3,1],[2,4]).

In the second test case, we can show that [3,1,2,4][3,1,2,4] is not the merge of two arrays of length 22.

In the third test case, [3,2,6,1,5,7,8,4]=merge([3,2,8,4],[6,1,5,7])[3,2,6,1,5,7,8,4]=merge([3,2,8,4],[6,1,5,7]).

In the fourth test case, [1,2,3,4,5,6]=merge([1,3,6],[2,4,5])[1,2,3,4,5,6]=merge([1,3,6],[2,4,5]), for example.

=======================-==================================================

首先应该发现的规律是,一个数字如果后面的数字比它小,那么它俩必须放在一组紧挨着的位置

比如  8 1

并且该数字其后面一直遍历,知道一个比它大的出现,否则都应该加入这一组

8 1 4 3 2 这种都应该加进8这一组

这样一来我们就完成了对数组的分块,转化成了01背包选与不选的问题,最终看是否能组成n的长度即可。还是很巧妙的一道题目

# include<bits/stdc++.h>

using namespace std;
# define mod 1000000007
typedef long long int ll;

int dp[4040];
int a[4040];
int b[4040];
int main ()
{

   int t;
   cin>>t;

   while(t--)
   {
       int n;
       cin>>n;

       for(int i=1;i<=2*n;i++)
       {
           cin>>a[i];
       }

       int now=0,len=0;
       for(int i=1;i<=2*n;i++)
       {
          if(i==2*n)
          {
              now=0;
              now++;
              len++;
              b[len]=now;
          }
          else
          {
              if(a[i]<a[i+1])
              {
                  len++;
                  b[len]=1;
              }
              else
              {
                  int pos=i;now=1;
                  for(int j=i+1;j<=2*n;j++)
                  {
                      if(a[j]<a[i])
                      {
                          pos=j;
                          now++;
                      }
                      else
                        break;
                  }
                  i=pos;
                  len++;
                  b[len]=now;
              }
          }
       }


    

       for(int i=1;i<=2*n;i++)
       {
           dp[i]=0;
       }
       dp[0]=1;

       for(int i=1;i<=len;i++)
       {
           for(int j=2*n;j>=b[i];j--)
           {
               dp[j]|=dp[j-b[i]];
           }
       }

       if(dp[n])
       {
           cout<<"YES"<<endl;
       }
       else
       {
           cout<<"No"<<endl;
       }
   }




    return 0;

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

秦三码

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

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

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

打赏作者

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

抵扣说明:

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

余额充值