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();
    }
  • 相关阅读:
    PHP 数组对象 按照某个字段进行排序
    laravel 多条件查询
    PHP QR CODE 类库生成二维码
    TypeError:Cannot read property 'type' of undefined
    input禁止输入的4种方法
    QQ会话调用地址
    禁止左右键复制
    设置Meta标签 清除页面缓存
    百度统计,百度自动推送合并
    强制使用360浏览器 使用webkit内核
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829328.html
Copyright © 2011-2022 走看看