题解 CF1496B

· · 题解

题目传送门

Analyse

设 a=\operatorname{mex}(S),b=\max(S),p=\left\lceil\dfrac{a+b}{2}\right\rceil。

那么显然有 a\ne b。

考虑分类讨论:

  1. 当 k=0 时,无操作,ans=n。

  2. 当 a<b 时,此时必然有 a<a+1<b,即 2\times a<a+b+1<2\times b,因此 a<p<b。所以此时向 S 中加入 p 对 a 和 b 的值是没有影响的。故 p 为定值,只需要考虑原集合中是否存在 p 这个值。如果是,ans=n;否则 ans=n+1。

  3. 当 a>b 时,此时必然有 a=b+1,故 p=\left\lceil\dfrac{2\times b+1}{2}\right\rceil=b+1。因此,每操作一次,S 中不同的数就会 +1。因此 ans=n+k。

Solution

比赛的时候脑抽了,用了个 multiset,不过还是能过的。

#include<bits/stdc++.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int M=1e5+5;
int T,n,k,x;
multiset<int> m;
multiset<int>::iterator it,st;
int main()
{
    scanf("%d",&T);
    while(T--)
    {
        m.clear();
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&x);
            m.insert(x);
        }
        if(k==0)
        {
            printf("%d\n",n);
            continue;
        }
        it=m.begin();
        int mex=-inf;
        if(*it>0)mex=0;// 无0就是0
        else
        {
            for(int i=1;i<n;i++)
            {
                st=it;it++;
                if(*st+1<*it){mex=i;break;}//找到mex
            }
            if(mex==-inf)mex=(*it)+1;//如果没找到,那么mex=max+1
        }
        it=m.end();
        it--;
        int mx=*it;
        if(mx+1==mex)
        {
            printf("%d\n",n+k);
            continue;
        }
        int ns=(int)ceil(0.5*(mex+mx));
        it=m.lower_bound(ns);
        if(*it!=ns)n++;//若不存在,ans=n+1
        printf("%d\n",n);
    }
    return 0;
}
The$ $end

感谢您的观看!如有不足,请麻烦指正哦。