• Codeforces Round #599 (Div. 2) A. Maximum Square


    Ujan decided to make a new wooden roof for the house. He has nn rectangular planks numbered from 11 to nn. The ii-th plank has size ai×1ai×1 (that is, the width is 11 and the height is aiai).

    Now, Ujan wants to make a square roof. He will first choose some of the planks and place them side by side in some order. Then he will glue together all of these planks by their vertical sides. Finally, he will cut out a square from the resulting shape in such a way that the sides of the square are horizontal and vertical.

    For example, if Ujan had planks with lengths 44, 33, 11, 44 and 55, he could choose planks with lengths 44, 33 and 55. Then he can cut out a 3×33×3 square, which is the maximum possible. Note that this is not the only way he can obtain a 3×33×3 square.

    What is the maximum side length of the square Ujan can get?

    Input

    The first line of input contains a single integer kk (1k101≤k≤10), the number of test cases in the input.

    For each test case, the first line contains a single integer nn (1n10001≤n≤1000), the number of planks Ujan has in store. The next line contains nn integers a1,,ana1,…,an (1ain1≤ai≤n), the lengths of the planks.

    Output

    For each of the test cases, output a single integer, the maximum possible side length of the square.

    Example
    input
    Copy
    4
    5
    4 3 1 4 5
    4
    4 4 4 4
    3
    1 1 1
    5
    5 5 1 1 5
    
    output
    Copy
    3
    4
    1
    3
    
    Note

    The first sample corresponds to the example in the statement.

    In the second sample, gluing all 44 planks will result in a 4×44×4 square.

    In the third sample, the maximum possible square is 1×11×1 and can be taken simply as any of the planks.

    //先从小打到排序,然后枚举最大边长 
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn =1005;
    int n,a[maxn];
    void solve() {
        scanf("%d",&n);
        for(int i=0; i<n; i++) {
            scanf("%d",&a[i]);
        }
        sort(a,a+n);//从小到大
        int ans = 0;
        for(int i=0; i<n; i++) {//从小开始
            for(int j=a[i]; j>=0; j--) {//让j为a[i]的高度,然后递减
                if((n-i)>=j) {// 如果大于a[i]的数目大于等于j,那么此时最大就是j
                    ans=max(ans,j);
                    break;
                }
            }
        }
        cout<<ans<<endl;
    }
    int main() {
        int t;
        scanf("%d",&t);
        while(t--)solve();
    }
  • 相关阅读:
    今天看了几个小时的微信小程序说说心得体会
    关于wordpress中的contact form7和WP Mail SMTP的一些设置
    关于163发邮件报错535 Error:authentication failed解决方法
    Numpy 基本除法运算和模运算
    基本的图像操作和处理
    Python中flatten用法
    media
    TensorFlow模型保存和提取方法
    docker 默认用户和密码
    Windows安装TensorFlow
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829328.html
Copyright © 2020-2023  润新知