Codeforces Round #764 (Div. 3) C. Division by Two and Permutation

题目链接:​​​​​​Problem - C - Codeforces

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 aiai with ⌊ai2⌋⌊ai2⌋, that is, by an integer part of dividing aiai 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 ⌊82⌋=4⌊82⌋=4, then a=[1,4,25,2]a=[1,4,25,2].
  2. Replace 2525 with ⌊252⌋=12⌊252⌋=12, then a=[1,4,12,2]a=[1,4,12,2].
  3. Replace 1212 with ⌊122⌋=6⌊122⌋=6, then a=[1,4,6,2]a=[1,4,6,2].
  4. Replace 66 with ⌊62⌋=3⌊62⌋=3, then a=[1,4,3,2]a=[1,4,3,2].

Input

The first line of input data contains an integer tt (1≤t≤1041≤t≤104) —the number of test cases.

Each test case contains exactly two lines. The first one contains an integer nn (1≤n≤501≤n≤50), the second one contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤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).

Example

input

Copy

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

output

Copy

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.

题意:你可以将数组的任意一位/=2(可以多次操作),问有没有可能将数组变成1-n的集合

思路:先将大于n的数字变成小于n的数字,然后一直跑for,将重复的数字/2,直到1出现多个就不行,如果恰好每个都有就可以

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

int arr[55];
bool vis[55];
int a[55];

int main(){
	ios::sync_with_stdio(false);
	int t;
	int n;
	cin >> t;
	while(t--){
		cin >> n;
		memset(vis, 0, sizeof(vis));
		memset(a, 0, sizeof(a));
		for(int i = 0; i < n; i++){
			cin >> arr[i];
			if(arr[i] <= n){
				vis[arr[i]] = 1;
			}
		}
		bool flag = 0;
		for(int i = 1; i <= n; i++){
			if(vis[i] == 0){
				flag = 1;
				break;
			}
		}
		if(!flag){
			cout << "YES" << endl;
		}else{
			for(int i = 0; i < n; i++){
				if(arr[i] <= n){
					a[arr[i]]++;
				}else{
					while(arr[i] > n){
						arr[i] /= 2;
					}
					a[arr[i]]++;
				}
			}
			while(flag){
				if(a[1] > 1){
					break;
				}
				for(int i = 0; i < n; i++){
					if(a[arr[i]] > 1 && arr[i] != 1){
						a[arr[i]]--;
						arr[i] /= 2;
						a[arr[i]]++;
					}
				}
				bool f = 0;
				for(int i = 1; i <= n; i++){
					if(a[i] != 1){
						f = 1;
						break;
					}
				}
				if(a[1] > 1){
					break;
				}
				if(f == 0){
					flag = 0;
					break;
				}
			}
			if(flag){
				cout << "NO" << endl;
			}else{
				cout << "YES" << endl;
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值