刷题|B - Young Explorers CodeForces - 1355B

【刷题|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 #include 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

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

    推荐阅读