Codeforces Round #645 (Div. 2) B. Maria Breaks the Self-isolation(贪心)
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 nn friends who are also grannies (Maria is not included in this number). The ii -th granny is ready to attend the ceremony, provided that at the time of her appearance in the courtyard there will be at least aiai other grannies there. Note that grannies can come into the courtyard at the same time. Formally, the granny ii 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 aiai .
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 11 ). All the remaining nn 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 aiai 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 ii -th granny in the moment of appearing in the courtyard, finds that now there are strictly less than aiai 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=6n=6 and a=[1,5,4,5,1,9]a=[1,5,4,5,1,9] , then:
- at the first step Maria can call grannies with numbers 11 and 55 , each of them will see two grannies at the moment of going out into the yard (note that a1=1≤2a1=1≤2 and a5=1≤2a5=1≤2 );
- at the second step, Maria can call grannies with numbers 22 , 33 and 44 , each of them will see five grannies at the moment of going out into the yard (note that a2=5≤5a2=5≤5 , a3=4≤5a3=4≤5 and a4=5≤5a4=5≤5 );
- the 66 -th granny cannot be called into the yard — therefore, the answer is 66 (Maria herself and another 55 grannies).
The first line contains a single integer tt (1≤t≤1041≤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 nn (1≤n≤1051≤n≤105 ) — the number of grannies (Maria is not included in this number).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤2⋅1051≤ai≤2⋅105 ).
It is guaranteed that the sum of the values nn over all test cases of the input does not exceed 105105 .
For each test case, print a single integer kk (1≤k≤n+11≤k≤n+1 ) — the maximum possible number of grannies in the courtyard.
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
6 1 6 4
这和前几天Edu87那场的B很类似。
大意就是n个人准备参加聚会,第i个人到来时必须已经有至少a[i]个人在场(这a[i]个人里也可以有和第i个人同一时刻来的),问最多能邀请到多少个人。
显然要先sort一遍,然后发现可以尝试安排让d[i]相同的人一起来,因为反正a[i]的值都一样,能多来一个是一个,尽可能贪心地把人数凑多。
所以我们要做的就是遍历数组,判断每组的最后一个人的a[i]的值是否小于等于i(因为如果a[i]相同的人的这一组都能去了,那么小于a[i]的人肯定也都得去),
如果是的话则更新答案为i+1(别忘了算上组织者)。
#include <bits/stdc++.h> using namespace std; int n,a[100005]; int main() { int t; cin>>t; while(t--) { cin>>n; int i; for(i=1;i<=n;i++)scanf("%d",&a[i]); a[n+1]=0x3f3f3f3f; sort(a+1,a+n+1); int cnt=1,j=0; int ans=1,step=0; for(i=1;i<=n;i++) { if(a[i]!=a[i+1]) { if(a[i]<=i)ans=max(ans,i+1); } } cout<<ans<<endl; } }