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

Posted qingyuyyyyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}

 

以上是关于Codeforces Round #599 (Div. 2) A. Maximum Square的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #599 (Div. 2) D. 0-1 MST(bfs+set)

Codeforces Round #599 (Div. 2) C. Tile Painting

Codeforces Round #599 Div2解题报告A-D

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

Codeforces Round #599 (Div. 2) B1. Character Swap (Easy Version)

Codeforces Round #599 (Div. 2)