zoukankan      html  css  js  c++  java
  • 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();
    }
  • 相关阅读:
    数据库——事务
    数据库——连接池
    JDBC——使用jdbc操作时,如何提取数据的性能
    2019-06-26 The Eclipse executable launcher was unable to locate its companion launcher
    2019-06-24在windows下如何查看80端口占用情况?是被哪个进程占用
    2019-06-19_解决ActiveMQ访问出现503的错误
    2019-06-17 tomcat启动;zookeeper启动批处理
    2019-06-13记忆 长命令
    2019-06-13linux的vi编辑器中如何查找关键字
    2019-06-13-vim配色步骤
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829328.html
Copyright © 2011-2022 走看看