CF--1624C.Division by Two and Permutation

这篇博客探讨了一种数组操作,通过将元素除以2并向下取整来转化为排列问题。给定一个包含正整数的数组,目标是通过有限次操作将其变为1到n的排列。博客中给出了具体的例子和解决思路,并展示了一个C++实现的解决方案,用于判断能否通过这些操作得到排列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

You are given an array aa consisting of nn positive integers. You can perform operations on it.

In one operation you can replace any element of the array a_iai​ with \lfloor \frac{a_i}{2} \rfloor⌊2ai​​⌋, that is, by an integer part of dividing a_iai​ by 22 (rounding down).

See if you can apply the operation some number of times (possible 00) to make the array aa become a permutation of numbers from 11 to nn —that is, so that it contains all numbers from 11 to nn, each exactly once.

For example, if a = [1, 8, 25, 2]a=[1,8,25,2], n = 4n=4, then the answer is yes. You could do the following:

  1. Replace 88 with \lfloor \frac{8}{2} \rfloor = 4⌊28​⌋=4, then a = [1, 4, 25, 2]a=[1,4,25,2].
  2. Replace 2525 with \lfloor \frac{25}{2} \rfloor = 12⌊225​⌋=12, then a = [1, 4, 12, 2]a=[1,4,12,2].
  3. Replace 1212 with \lfloor \frac{12}{2} \rfloor = 6⌊212​⌋=6, then a = [1, 4, 6, 2]a=[1,4,6,2].
  4. Replace 66 with \lfloor \frac{6}{2} \rfloor = 3⌊26​⌋=3, then a = [1, 4, 3, 2]a=[1,4,3,2].

Input

The first line of input data contains an integer tt (1 \le t \le 10^41≤t≤104) —the number of test cases.

Each test case contains exactly two lines. The first one contains an integer nn (1 \le n \le 501≤n≤50), the second one contains integers a_1, a_2, \dots, a_na1​,a2​,…,an​ (1 \le a_i \le 10^91≤ai​≤109).

Output

For each test case, output on a separate line:

  • YES if you can make the array aa become a permutation of numbers from 11 to nn,
  • NO otherwise.

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

Sample 1

InputcopyOutputcopy
6
4
1 8 25 2
2
1 1
9
9 8 3 4 2 7 1 5 6
3
8 2 1
4
24 7 16 7
5
22 6 22 4 22
YES
NO
YES
NO
NO
YES

Note

The first test case is explained in the text of the problem statement.

In the second test case, it is not possible to get a permutation.

解决了我的疑惑:能放却不放,先去补更小的空位这个做法。感谢Codeforces 1624C Division by Two and Permutation_机械之忍的博客-优快云博客

1 8 25 2

--------

1:1

8:8 4 2 1

25: 25 12 6 3 1

YES

22 6 22 4 22

-------

22:22 11 5 2 1

6:6 3 1

22:22 11 5 2 1

4: 4 2 1

22:22 11 5 2 1

YES

#include<bits/stdc++.h>
using namespace std;
bool book[60];

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>n;
		int cnt=0;
		memset(book,false,sizeof(book));
		
		for(int i=1;i<=n;i++)
		{
			int x;
			cin>>x;
			while(x>n)x/=2;
			
			while(x)
			{
				if(!book[x])
				{
					book[x]=true;
					cnt++;
					break;
				}
				x/=2;
			} 
		}
		if(cnt==n)
		cout<<"YES"<<endl;
		else
		cout<<"NO"<<endl;
		
	}
	return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值