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

时间:2019-11-10 11:46:16   收藏:0   阅读:66

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();
}

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!