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 (1≤k≤101≤k≤10), the number of test cases in the input.

For each test case, the first line contains a single integer nn (1≤n≤10001≤n≤1000), the number of planks Ujan has in store. The next line contains nn integers a1,…,ana1,…,an (1≤ai≤n1≤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();
}

原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11829328.html

时间: 2024-10-30 17:54:00

Codeforces Round #599 (Div. 2) A. Maximum Square的相关文章

Codeforces Round #249 (Div. 2) A. Black Square

水题 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a(4); cin >> a[0] >> a[1]>>a[2]>>a[3]; string str; cin >> str; int res = 0; for(int i = 0 ; i

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

Codeforces Round #599 (Div. 2) D. 0-1 MST Description Ujan has a lot of useless stuff in his drawers, a considerable part of which are his math notebooks: it is time to sort them out. This time he found an old dusty graph theory notebook with a descr

Codeforces Round #599 (Div. 2)

A - Maximum Square 题意:给 \(n\) 块宽度为 \(1\) 长度为 \(a_i\) 的木板,把这些木板拼在一起,求最大形成的正方形的边长. 题解:贪心,从大到小排序,然后找第一个满足 \(a_i<i\) 的位置break掉. #include<bits/stdc++.h> using namespace std; typedef long long ll; int n, a[1005]; int main() { #ifdef KisekiPurin freopen

Codeforces Round #221 (Div. 1) B. Maximum Submatrix 2 dp排序

B. Maximum Submatrix 2 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/375/B Description You are given a matrix consisting of digits zero and one, its size is n × m. You are allowed to rearrange its rows. What is

Codeforces Round #221 (Div. 2) D. Maximum Submatrix 2 (思维题)

题目地址:codeforces 221 D 这场是人生中做的第一场CF中的D题.(当时只做出来了A题..)过年之际回顾了一下,就顺便看了几道D题.现在做CF的D题在比赛时还是做不出来.但是赛后往往都可以自己做出来.据说D题能在比赛中稳出的话就可以区域赛银了.于是争取以后CF能稳出4道题吧. 这道题刚开始不该看标签的..给的是DP..于是就一直朝着DP方向想.但是感觉不像是DP.就换了个思路,就做出来了. 大体方法是先预处理出每一行中每个数向左延伸最长的连续1的个数.然后对每一行的进行排序(我这里

Codeforces Round #172 (Div. 2)---D. Maximum Xor Secondary(RMQ + 二分)

Bike loves looking for the second maximum element in the sequence. The second maximum element in the sequence of distinct numbers x1,?x2,?-,?xk (k?>?1) is such maximum element xj, that the following inequality holds: . The lucky number of the sequenc

Codeforces Round #599 (Div. 2)D 边很多的只有0和1的MST

题:https://codeforces.com/contest/1243/problem/D 分析:找全部可以用边权为0的点连起来的全部块 然后这些块之间相连肯定得通过边权为1的边进行连接 所以答案就是这些块的总数-1: #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back const int M=1e5+5; set<int>s,g[M]; int vis[

C. Tile Painting (定理:任意一个合数都能够写成两个质数的乘积) 《Codeforces Round #599 (Div. 2) 》

Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of nn consecutive tiles, numbered from 11 to nn. Ujan will paint each tile in some color

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

Ujan has been lazy lately, but now has decided to bring his yard to good shape. First, he decided to paint the path from his house to the gate. The path consists of nn consecutive tiles, numbered from 11 to nn. Ujan will paint each tile in some color