【CF】Codeforces 1702F

本文介绍了一种名为EquateMultisets的问题,涉及如何通过有限次数的元素加倍或减半操作,使得两个多集(允许重复元素且顺序不重要)变得相等。通过实例演示了算法步骤和关键操作的使用,以及如何判断是否可能达成目标。
摘要由CSDN通过智能技术生成

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 \cdot 2 x2 ,
  • or replace x x x with ⌊ x 2 ⌋ \lfloor \frac{x}{2} \rfloor 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 = { 4 , 24 , 5 , 2 } a = \{4, 24, 5, 2\} a={4,24,5,2} , b = { 4 , 1 , 6 , 11 } b = \{4, 1, 6, 11\} b={4,1,6,11} , then the answer is yes. We can proceed as follows:

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

输入格式

The first line of input data contains a single integer t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104 ) —the number of test cases.

Each test case consists of three lines.

The first line of the test case contains an integer n n n ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 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 a_1, a_2, \dots, a_n a1,a2,,an ( 1 ≤ a 1 ≤ a 2 ≤ ⋯ ≤ a n ≤ 1 0 9 1 \le a_1 \le a_2 \le \dots \le a_n \le 10^9 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 b_1, b_2, \dots, b_n b1,b2,,bn ( 1 ≤ b 1 ≤ b 2 ≤ ⋯ ≤ b n ≤ 1 0 9 1 \le b_1 \le b_2 \le \dots \le b_n \le 10^9 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 \cdot 10^5 2105 .

输出格式

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).

样例 #1

样例输入 #1

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

样例输出 #1

YES
NO
YES
YES
YES

提示

The first example is explained in the statement.

In the second example, it is impossible to get the value $ 31 $ from the numbers of the multiset $ b $ by available operations.

In the third example, we can proceed as follows:

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

中文翻译

给你两个长度为 n n n 的数组 a a a 和数组 b b b , 定义x的一次操作 (二选一) : :

  • x x x 替换成 x ⋅ 2 x \cdot 2 x2 ,

  • x x x 替换成 ⌊ x 2 ⌋ \lfloor \frac{x}{2} \rfloor 2x (下取整).

a a a 数组不能做操作, b b b 数组不能做操作。

问经过若干操作,能否讲 b b b 数组 变成 a a a 数组? (数的顺序可以不一样)。

题解

先把 a a a 数组一直除以 2 2 2 (能除得尽就除) ,用一个 m a p map map 统计个数,然后贪心地处理每个 b i b_i bi ,能用就用。

#include<bits/stdc++.h>
using namespace std;
int n,a[200010],b[200010];
map<int,int> mp;
void solve(){
	mp.clear();
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		while(a[i]%2==0){
			a[i]/=2;
		}
		mp[a[i]]++;
	}
	for(int i=1;i<=n;i++)cin>>b[i];
	for(int i=1;i<=n;i++){
		int tmp=b[i];
		bool flag=0;
		while(tmp){
			if(!mp[tmp]){
				tmp/=2;
				continue;
			}
			mp[tmp]--;
			flag=1;
			break;
		}
		if(!flag){
			puts("NO");
			return ;
		}
	}
	for(auto it:mp){
		if(it.second>0){
			puts("NO");
		}
	}
	puts("YES");
	return ;
}
int main(){
	int TTT;
	cin>>TTT;
	while(TTT--){
		solve();
	}
	return 0;
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值