Codeforces Round 858 (Div. 2) B. Mex Master

给定一个数组,允许重新排列其元素,目标是找到新序列中相邻元素之和未出现的最小非负整数的最小可能值。关键在于分析0的个数与非0个数的关系,以确定最小MEX。当0的个数少于非0个数的两倍时,答案为0;否则根据最大元素的值决定答案为1或2。
摘要由CSDN通过智能技术生成

题目传送门

B. Mex Master

time limit per test1 second

memory limit per test1024 megabytes

inputstandard input

outputstandard output

You are given an array a of length n. The score of a is the MEX of [a1+a2,a2+a3,…,an−1+an]. Find the minimum score of a if you are allowed to rearrange elements of a in any order. Note that you are not required to construct the array a that achieves the minimum score.

†† The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1] is 0, because 0 does not belong to the array.

  • The MEX of [3,1,0,1] is 2 , because 0 and 1 belong to the array, but 2 does not.

  • The MEX of [0,3,1,2] is 4 because 0, 1, 2, and 3 belong to the array, but 4 does not.

Input

The first line contains a single integer t (1≤t≤10^4) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer n (2≤n≤2⋅10^5).

The second line of each test case contains n integers a1,a2,…,an(0≤ai≤2⋅10^5).

It is guaranteed that the sum of n over all test cases does not exceed 2⋅10^5.

Output

For each test case, output the minimum score of a after rearranging the elements of a in any order.

Example

3
2
0 0
3
0 0 1
8
1 0 0 0 2 0 3 0

output

1
0
1

Note

In the first test case, it is optimal to rearrange a as [0,0], the score of this array is the MEX of [0+0]=[0], which is 1.

In the second test case, it is optimal to rearrange a as [0,1,0]], the score of this array is the MEX of [0+1,1+0]=[1,1], which is 0.

大概题意:给定一个长度为n的数组,可以以任意顺序排列元素。求出所有排列中每两个相邻元素之和组成的新序列中未出现的最小的非负整数的最小值。

思路:此题的重点在0的个数。(为了方便举栗子,后面用1代表非0数)

  • 当0的个数-非0的个数<2时,我们能组成所有元素都大于0的序列,则此时答案为0。

例如:1 1 1 0 0. 我们可以将0插入到1的中间,形如 0 1 0 1 1 1 ,则相邻的元素和组成的序列为

[1 1 1 2 2],显然结果为0. 那么临界值为什么是2呢,因为在每两个相邻1中只能插入一个0,那么最多可以插入的0数为 非0数+1 ,形如 0 1 0 1 0 1 0……1 0;

  • 当0的个数-非0的个数>=2时,则需分两种情况来判断:

这种情况则无论如何0都会存在。

  1. 最大的元素为1时,答案为2. 结果最小的情况则是以“0 1 0 1....”的格式循环排列,最后将多出来无法组合的0放在在前面0 0 0 1 0 1 0 1 0 1......(感谢大佬的指正,蒟蒻发抖)

  1. 其他情况答案为1. 这里将最大值假设为2 最小结果排列形如: 0 0 0 0 2 1 1 1 1......

这样在最大值与0的交界前 都为0,交界处为最大值>1 ,而后面每个元素>=1,他们两数之和>=2;

AC代码

#include<bits/stdc++.h>


using namespace std;


const int N=2e5+10;

#define ll long long 
int a[N];

void solve(){
    int n;
    cin>>n;
    int num0=0,num=0;
    int maxn=0;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]==0){
            num0++;
        }
        else{
            num++;
        }
        maxn=max(maxn,a[i]);
    }
    if(num0-num<2){
        cout<<0<<endl;
    }
    else{
        if(maxn==1){
                cout<<2<<endl;
        }
        else{
            cout<<1<<endl;
        }
    } 
}




int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t;
    cin>>t;
    while(t--){
        solve();
    } 
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值