Maria Breaks the Self-isolation(CodeForces-645-Div2-B)

B. Maria Breaks the Self-isolation
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Maria is the most active old lady in her house. She was tired of sitting at home. She decided to organize a ceremony against the coronavirus.

She has n friends who are also grannies (Maria is not included in this number). The i-th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least ai other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny i agrees to come if the number of other grannies who came earlier or at the same time with her is greater than or equal to ai.

Grannies gather in the courtyard like that.

Initially, only Maria is in the courtyard (that is, the initial number of grannies in the courtyard is 1). All the remaining n grannies are still sitting at home.
On each step Maria selects a subset of grannies, none of whom have yet to enter the courtyard. She promises each of them that at the time of her appearance there will be at least ai other grannies (including Maria) in the courtyard. Maria can call several grannies at once. In this case, the selected grannies will go out into the courtyard at the same moment of time.
She cannot deceive grannies, that is, the situation when the i-th granny in the moment of appearing in the courtyard, finds that now there are strictly less than ai other grannies (except herself, but including Maria), is prohibited. Please note that if several grannies appeared in the yard at the same time, then each of them sees others at the time of appearance.
Your task is to find what maximum number of grannies (including herself) Maria can collect in the courtyard for the ceremony. After all, the more people in one place during quarantine, the more effective the ceremony!

Consider an example: if n=6 and a=[1,5,4,5,1,9], then:

at the first step Maria can call grannies with numbers 1 and 5, each of them will see two grannies at the moment of going out into the yard (note that a1=1≤2 and a5=1≤2);
at the second step, Maria can call grannies with numbers 2, 3 and 4, each of them will see five grannies at the moment of going out into the yard (note that a2=5≤5, a3=4≤5 and a4=5≤5);
the 6-th granny cannot be called into the yard — therefore, the answer is 6 (Maria herself and another 5 grannies).
Input
The first line contains a single integer t (1≤t≤104) — the number of test cases in the input. Then test cases follow.

The first line of a test case contains a single integer n (1≤n≤105) — the number of grannies (Maria is not included in this number).

The second line contains n integers a1,a2,…,an (1≤ai≤2⋅105).

It is guaranteed that the sum of the values n over all test cases of the input does not exceed 105.

Output
For each test case, print a single integer k (1≤k≤n+1) — the maximum possible number of grannies in the courtyard.

Example
inputCopy
4
5
1 1 2 2 1
6
2 3 4 5 6 7
6
1 5 4 5 1 9
5
1 2 3 5 6
outputCopy
6
1
6
4
Note
In the first test case in the example, on the first step Maria can call all the grannies. Then each of them will see five grannies when they come out. Therefore, Maria and five other grannies will be in the yard.

In the second test case in the example, no one can be in the yard, so Maria will remain there alone.

The third test case in the example is described in the details above.

In the fourth test case in the example, on the first step Maria can call grannies with numbers 1, 2 and 3. If on the second step Maria calls 4 or 5 (one of them), then when a granny appears in the yard, she will see only four grannies (but it is forbidden). It means that Maria can’t call the 4-th granny or the 5-th granny separately (one of them). If she calls both: 4 and 5, then when they appear, they will see 4+1=5 grannies. Despite the fact that it is enough for the 4-th granny, the 5-th granny is not satisfied. So, Maria cannot call both the 4-th granny and the 5-th granny at the same time. That is, Maria and three grannies from the first step will be in the yard in total.

题意:有n个人,他们每一个人都有要求,在他们到达时,必须之前有比他们要求的人数多或者相等的人数,才能成立,否则,他就无法前来,院子里还有一名志愿者,算上他。找到可以来最多的人数
思路:一开始思路有点混乱,自己也往那个方面想了,看了一位大佬的,思路差不多,看来以后还是得把自己的思路多想想。
将数组从小到大排序,写出对应的人数,举个例子:
人数:1 2 3 4 5 6
要求:1 1 4 5 5 9
从后往前来看,这样就能避免一些问题,因为他要找的是最多的人数,人数为6时,要求是9,看前一位人数是5时,再加一位志愿者就是6,也就是i了,所以如果i>=ai,就代表可以成立,如果没有可以成立的结果,就只有志愿者一个人。
代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
#include<string>
using namespace std;
const int N=1e5+9;
int a[N];
int main ()
{
    int t;
    ios::sync_with_stdio(false);
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        for(int i=1;i<=n;i++){
            cin>>a[i];
        }
        sort(a+1,a+n+1);
        int i;
        for(i=n;i>=1;i--){
            if(i>=a[i]){
                break;
            }
        }
        cout<<i+1<<endl;//输出人数+志愿者
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值