CF1358B Maria Breaks the Self-isolation

题目链接

题意

这是一个集会,最开始只有你一个人。

你邀请了 \(n\) 个人来参加集会,每个人有一个 \(a_i\) ,如果你要求这个人 \(t\) 时刻过来,那么必须满足 \(t\) 时刻以及 \(t\) 时刻之前来的人的个数 \(\geq a_i\) (注意对于这个人数的计算,包括你自己,但是对于 \(i\) 来说,不包括他)

求出最多有多少个人能来集会。

题解

  • 先对a数组排序
  • 记当前已经在集会中的人数为cnt,当前待参加集会的人数为wait,对每一个a[i],若cnt+wait >= a[i],则满足条件,集会中的人数增加a[i]+1个人;若不满足条件,则等待人数加一
const int N=1e5+10;
int a[N];
int n;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;

        for(int i=0;i<n;i++) cin>>a[i];

        sort(a,a+n);

        int cnt=1,wait=0;
        for(int i=0;i<n;i++)
        {
            if(cnt+wait >= a[i])
            {
                cnt+=wait+1;
                wait=0;
            }
            else wait++;
        }

        cout<<cnt<<endl;
    }
    //system("pause");
}
posted @ 2020-08-30 17:45  Dazzling!  阅读(194)  评论(0编辑  收藏  举报