CF 1702F - Equate Multisets

Equate Multisets

Multiset —is a set of numbers in which there can be equal elements, and the order of the numbers does not matter. Two multisets are equal when each value occurs the same number of times. For example, the multisets { 2 , 2 , 4 2,2,4 2,2,4} and { 2 , 4 , 2 2,4,2 2,4,2} are equal, but the multisets { 1 , 2 , 2 1,2,2 1,2,2} and { 1 , 1 , 2 1,1,2 1,1,2} — are not.
You are given two multisets a a a and b b b, each consisting of n n n integers.
In a single operation, any element of the b b b multiset can be doubled or halved (rounded down). In other words, you have one of the following operations available for an element x x x of the b b b multiset:

  • replace x x x with x ⋅ 2 x⋅2 x2,
  • or replace x x x with ⌊ x 2 ⌋ ⌊\frac x 2⌋ 2x (round down).

Note that you cannot change the elements of the a a a multiset.
See if you can make the multiset b b b become equal to the multiset a a a in an arbitrary number of operations (maybe 0 0 0).
For example, if n = 4 n=4 n=4, a = a= a={ 4 , 24 , 5 , 2 4,24,5,2 4,24,5,2}, b = b= b={ 4 , 1 , 6 , 11 4,1,6,11 4,1,6,11}, then the answer is yes. We can proceed as follows:

  • Replace 1 1 1 with 1 ⋅ 2 = 2 1⋅2=2 12=2. We get b = b= b={ 4 , 2 , 6 , 11 4,2,6,11 4,2,6,11}.
  • Replace 11 11 11 with ⌊ 11 2 ⌋ = 5 ⌊\frac {11} 2⌋=5 211=5. We get b = b= b={ 4 , 2 , 6 , 5 4,2,6,5 4,2,6,5}.
  • Replace 6 6 6 with 6 ⋅ 2 = 12 6⋅2=12 62=12. We get b = b= b={ 4 , 2 , 12 , 5 4,2,12,5 4,2,12,5}.
  • Replace 12 12 12 with 12 ⋅ 2 = 24 12⋅2=24 122=24. We get b = b= b={ 4 , 2 , 24 , 5 4,2,24,5 4,2,24,5}.
  • Got equal multisets a = a= a={ 4 , 24 , 5 , 2 4,24,5,2 4,24,5,2} and b = b= b={ 4 , 2 , 24 , 5 4,2,24,5 4,2,24,5}.

 
Input
The first line of input data contains a single integer t ( 1 ≤ t ≤ 1 0 4 ) t (1≤t≤10^4) t(1t104) —the number of test cases.
Each test case consists of three lines.
The first line of the test case contains an integer n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 ) n (1≤n≤2⋅10^5) n(1n2105) —the number of elements in the multisets a a a and b b b.
The second line gives n n n integers: a 1 , a 2 , … , a n ( 1 ≤ a 1 ≤ a 2 ≤ ⋯ ≤ a n ≤ 1 0 9 ) a_1,a_2,…,a_n (1≤a_1≤a_2≤⋯≤a_n≤10^9) a1,a2,,an(1a1a2an109) —the elements of the multiset a a a. Note that the elements may be equal.
The third line contains n n n integers: b 1 , b 2 , … , b n ( 1 ≤ b 1 ≤ b 2 ≤ ⋯ ≤ b n ≤ 1 0 9 ) b_1,b_2,…,b_n (1≤b_1≤b_2≤⋯≤b_n≤10^9) b1,b2,,bn(1b1b2bn109) — elements of the multiset b b b. Note that the elements may be equal.
It is guaranteed that the sum of n n n values over all test cases does not exceed 2 ⋅ 1 0 5 2⋅10^5 2105.
 
Output
For each test case, print on a separate line:

  • YES if you can make the multiset b b b become equal to a a a,
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as positive answer).
 
Example
input

5
4
2 4 5 24
1 4 6 11
3
1 4 17
4 5 31
5
4 7 10 13 14
2 14 14 26 42
5
2 2 4 4 4
28 46 62 71 98
6
1 2 10 16 64 80
20 43 60 74 85 99

output

YES
NO
YES
YES
YES

Note
The first example is explained in the statement.
In the second example, it is impossible to get the value 31 31 31 from the numbers of the multiset b b b by available operations.
In the third example, we can proceed as follows:

  • Replace 2 2 2 with 2 ⋅ 2 = 4 2⋅2=4 22=4. We get b = b= b={ 4 , 14 , 14 , 26 , 42 4,14,14,26,42 4,14,14,26,42}.
  • Replace 14 14 14 with ⌊ 14 2 ⌋ = 7 ⌊\frac {14} 2⌋=7 214=7. We get b = b= b={ 4 , 7 , 14 , 26 , 42 4,7,14,26,42 4,7,14,26,42}.
  • Replace 26 26 26 with ⌊ 26 2 ⌋ = 13 ⌊\frac {26} 2⌋=13 226=13. We get b = b= b={ 4 , 7 , 14 , 13 , 42 4,7,14,13,42 4,7,14,13,42}.
  • Replace 42 42 42 with ⌊ 42 2 ⌋ = 21 ⌊\frac {42} 2⌋=21 242=21. We get b = b= b={ 4 , 7 , 14 , 13 , 21 4,7,14,13,21 4,7,14,13,21}.
  • Replace 21 21 21 with ⌊ 21 2 ⌋ = 10 ⌊\frac {21} 2⌋=10 221=10. We get b = b= b={ 4 , 7 , 14 , 13 , 10 4,7,14,13,10 4,7,14,13,10}.
  • Got equal multisets a = a= a={ 4 , 7 , 10 , 13 , 14 4,7,10,13,14 4,7,10,13,14} and b = b= b={ 4 , 7 , 14 , 13 , 10 4,7,14,13,10 4,7,14,13,10}.

题目大意
给你两个 n n n 个元素集合 a a a b b b (非降序),以集合中的出现的数及其出现次数来表示相不相同。能否把集合 b b b 进行以下操作:

  • 将某个元素乘 2 2 2
  • 就某个元素除以 2 2 2 ,并向下取整。

变得与集合 a a a 相同,每个元素都可以操作任意次(也可以是 0 0 0 次),能就输出YES,否则输出NO

解题思路
显然,任何一个偶数都存在一个数乘 2 2 2 得到。比较难得到的其实是奇数,那么我们就可以把集合 a a a 中的元素都变成我们所需要的奇数,也就是将不是奇数,不断地进行除以 2 2 2 操作变成奇数。接下来,我们只需要考虑集合 b b b 的元素能否进行除以 2 操作得到集合 a a a 所需要的奇数。

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

int main()
{
    int t; cin>>t;
    while(t--)
    {
        int n; cin>>n;
        map<int, int> need; //记录需要的奇数及其个数
        for(int i=0; i<n; i++)
        {
            int x; cin>>x;
            while(x%2 == 0) x /= 2; //将偶数转化为所需的奇数 (由该奇数不断乘 2 得到的偶数
            need[x]++;
        }
        int cnt = 0; //记录集合 b 中能够转化到集合 a 的元素个数
        bool flag = false;
        for(int i=0; i<n; i++)
        {
            int x; cin>>x;
            while(x)
            {
                if(x%2 && need[x]) //转化到一个奇数. 并且是需要的奇数
                {
                    cnt++; //转化成功数 +1
                    need[x]--; //该奇数需要 -1
                    break;
                }
                x /= 2;
            }
        }
        if(cnt == n) flag = true; //都有对应的转化的奇数
        cout<<(flag? "YES\n": "NO\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

花生ono

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

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

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

打赏作者

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

抵扣说明:

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

余额充值