Division by Two and Permutation 贪心,模拟(1100) Codeforces Round #764 (Div. 3)

C. Division by Two and Permutation
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given an array 𝑎 consisting of 𝑛 positive integers. You can perform operations on it.

In one operation you can replace any element of the array 𝑎𝑖 with ⌊𝑎𝑖2⌋, that is, by an integer part of dividing 𝑎𝑖 by 2 (rounding down).

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

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

Replace 8 with ⌊82⌋=4, then 𝑎=[1,4,25,2].
Replace 25 with ⌊252⌋=12, then 𝑎=[1,4,12,2].
Replace 12 with ⌊122⌋=6, then 𝑎=[1,4,6,2].
Replace 6 with ⌊62⌋=3, then 𝑎=[1,4,3,2].
Input
The first line of input data contains an integer 𝑡 (1≤𝑡≤104) —the number of test cases.

Each test case contains exactly two lines. The first one contains an integer 𝑛 (1≤𝑛≤50), the second one contains integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤109).

Output
For each test case, output on a separate line:

YES if you can make the array 𝑎 become a permutation of numbers from 1 to 𝑛,
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
inputCopy
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
outputCopy
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.

题意 :

  • 给n个正整数,每次操作可以选择一个数除以二并向下取整,问进行若干次(0)操作后,能否使这n个数成为n的全排列(不需要顺序)

思路 :

  • n个数,说明每个数最后都要转换成这个n的全排列中的一个,所以如果一开始大于n,则先转为小于等于n;n的全排列,说明1-n有且仅有出现1次每个数只能从大往小转化,不能从小往大转化,因此从n开始枚举,如果个数为0,直接失败,如果个数大于1,只留下一个其他往下转化
  • 首先将这n个数都转换为小于等于n的状态,并记录每个值对应的个数
  • 从n到1枚举,如果i的个数为0,直接失败,如果个数大于1,只留下一个,其他的都进行除以二的操作
  • 从1到n枚举,如果所有值对应的个数都恰好为1,成功,反之则失败
#include <iostream>
#include <cstring>
#define endl '\n'
using namespace std;

const int N = 55;

int tag[N];

inline void solve()
{
    memset(tag, 0, sizeof tag);
    int n; cin >> n;
    for (int i = 0, x; i < n && cin >> x; i ++ )
    {
        while (x > n) x >>= 1;
        tag[x] ++ ;
    }
    
    for (int i = n; i; i -- )
    {
        if (tag[i] == 0){cout << "NO" << endl; return ;}
        while (tag[i] > 1) tag[i / 2] ++ ,tag[i] -- ;
    }
    
    for (int i = 1; i <= n; i ++ )
        if (tag[i] != 1)
        {
            cout << "NO" << endl;
            return ;
        }
    cout << "YES" << endl;
}

int main()
{
    ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    int _; cin >> _;
    
    while (_ -- )
        solve();
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值