【Codeforces Round #645 (Div. 2) B】Maria Breaks the Self-isolation

题目链接

【题目翻译】

每个奶奶有个编号a[i]

只有当操场上除了自己然后人数大于等于a[i]的时候,她才能出去。

(同时一起出去x个人的话,其余x-1个人也算是已经到操场上了)

然后问你最多能出去多少个人到操场上。

【题解】

这题不要想复杂了,你就直接想最后操场上有多少个人。设为x。

那么肯定对于操场上每个人都应该满足a[i]<=x-1。

所以肯定可以一次性都到操场上去的,不用一次一次出去一拨人。

所以我们可以把a数组排个序,然后逆序枚举最大值x,找到a[i]<=x的最大的i,则x+1肯定就是答案了。

(排序让每次的a[i]都肯定是i个人的情况下最小的,逆序枚举找最大的!)

【代码】

#include<bits/stdc++.h>
#define ll long long
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;

const int N = 1e5;

int n,m;
int a[N+10];

int main(){
    #ifdef LOCAL_DEFINE
        freopen("D:\rush.txt","r",stdin);
    #endif
    int T;
    rei(T);
    while (T--){
        rei(n);
        rep1(i,1,n) rei(a[i]);
        sort(a+1,a+1+n);
        int ans = 1;
        rep2(x,n,1){
            if (x>=a[x]){
                ans = x+1;
                break;
            }
        }
        printf("%d
",ans);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/AWCXV/p/12970206.html