B - Young Explorers CodeForces - 1355B

 传送

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter eiei — his inexperience. Russell decided that an explorer with inexperience ee can only join the group of ee or more people.

Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.

Input

The first line contains the number of independent test cases TT(1≤T≤2⋅1051≤T≤2⋅105). Next 2T2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers NN (1≤N≤2⋅1051≤N≤2⋅105).

The second line contains NN integers e1,e2,…,eNe1,e2,…,eN (1≤ei≤N1≤ei≤N), where eiei is the inexperience of the ii-th explorer.

It's guaranteed that sum of all NN doesn't exceed 3⋅1053⋅105.

Output

Print TT numbers, each number on a separate line.

In ii-th line print the maximum number of groups Russell can form in ii-th test case.

Example

Input

2
3
1 1 1
5
2 3 1 2 2

Output

3
2

Note

In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to 11, so it's not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 11, 22 and 33 will form the first group, and the other two explorers with inexperience equal to 22 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 22, and the second group using only one explorer with inexperience equal to 11. In this case the young explorer with inexperience equal to 33 will not be included in any group.

题意:t组数据,每组给出n个人,每个人都有对应的经验值e。现要将这些人分组(部分人可以不划分到任何一组中,相当于孤儿了【不是】),要求经验值e的人划分的队伍中至少有e人。例如(2,3,3)这样的划分是合理的,但是(2,3)这样的划分是不合理的,队伍中出现了一个e=3人,他出现的团队必须至少有3人。求最多可以分成几组人。

题解:为了使组别尽可能多,则组中人的e值应该尽可能低,所以部分e值特别高的大哥可以让他们当”孤儿“。然后操作就是先将这些e值从小到大排序,先将e值低的人分组,人数足够多再考虑e值高的人群。麻烦的是,你想把某个e划分进组,你需要你只是否拥有e个e值小于等于它的人(有点绕,举例就是如果我想把某个e值为3的人划分进组,那我至少还需要2个e值小于等于3的人,因为e值比3大的话,会扩大这个队伍的容量)。解释困难,看代码应该能懂。

#include<iostream>
#include<algorithm>
using namespace std;

int t, n, a[200005];
int main(){
	scanf("%d", &t);
	while(t--){
		int sum=0;
		scanf("%d", &n);
		for(int i=0; i<n; i++)
			scanf("%d", a+i);
			
		sort(a, a+n);//从小到大排序 
		int flag = 0;//用来记录下标,表示从下标0到下标j的人,已被我划分进组 
		for(int i=0; i<n; ){//a[i]即第i个人的e值大小,此处用来表示队伍容量,后续招人要保证所有人的e值≤a[i] 
			if(a[i] <= n-flag){	
//n-flag表示还未分组的人,在不考虑剩余人e值情况下,先判断人数是否达到我现在的分组需要 
				for(int j=i; j<n; j++){
					if(a[j] != a[i]){//因为提前排序好,一旦a[j]!=a[i],表示此人的e值增大,如果将他分进组会扩大队伍的容量 
						i++;
						break;//这样的话a[i]大小的队伍就不能组成,break出循环,增大队伍容量之后继续判断 
					}
					if(j-flag+1 == a[i]){//j-flag+1表示e值≤a[j]并且还未分组的人,此处a[i]==a[j] 
						sum++;
						flag = i = j+1;//分组以后更新下标flag 
						break;
					}
				}
			}else
				break;
		}
		printf("%d\n", sum);
	}
	return 0;
}

因为我有点笨,没办法很好地传达我的意思,所以我在代码中加入了大量的文字注释,希望可以帮助理解。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值