CodeForces 1355 B. Young Explorers#
目录#
1. 题目描述#
1.1. Limit#
Time Limit: 2 seconds
Memory Limit: 256 megabytes
1.2. Problem Description#
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 — his inexperience. Russell decided that an explorer with inexperience can only join the group of 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.
1.3. Input#
The first line contains the number of independent test cases (). Next lines contain description of test cases.
The first line of description of each test case contains the number of young explorers ().
The second line contains integers (), where is the inexperience of the -th explorer.
It's guaranteed that sum of all doesn't exceed .
1.4. Output#
Print numbers, each number on a separate line.
In -th line print the maximum number of groups Russell can form in -th test case.
1.5. Sample Input#
2
3
1 1 1
5
2 3 1 2 2
1.6. Sample Output#
3
2
1.7. 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 , so it's not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience , and will form the first group, and the other two explorers with inexperience equal to 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 , and the second group using only one explorer with inexperience equal to . In this case the young explorer with inexperience equal to will not be included in any group.
1.8. Source#
CodeForces 1355 B. Young Explorers
2. 解读#
首先考虑数据范围,一共有个测试用例(),每个用例有个数字 ()。
如果每个测试用例需要进行 次运算,那么一共需要进行 次运算,对比一般笔记本电脑一秒钟大约 次运算,这个算法非常容易超时。
我在一开始提交的时候,在循环中加了一个 语句来初始化数组,这个函数的复杂度是 的,这里的 表示数组长度,即 ,结果就超时了。
在去掉 语句以后,如果每个测试用例都对输入的 个元素进行一次遍历计算,也能够通过,应该是测试用例没有每个 都设置到 这么大。不过还是推荐省去一些不必要的计算,更加稳妥一些。
在读取输入以后,先对 个数从小到大进行排序,对 进行遍历,用 表示能够用来组建新队伍的人数, 初始化为1。
若 ,则 , 。
若 ,则用 存储 ,,,表示直接使用后面的人对 队伍要求人数进行补充,直到队伍人数满足要求,跳过中间 个数,节约了一些计算。
3. 代码#
#include <algorithm>
#include <iostream>
#include <string.h>
const int num = 2 * 1e5 + 1;
using namespace std;
int list[num];
int main()
{
int t;
int n, mark;
long long ans;
// test case
scanf("%d", &t);
// for each test case
while (t--) {
scanf("%d", &n);
// 初始化
ans = 0;
mark = 1;
// 这里不能用memset,会超时
// memset(list, 0, sizeof(list));
// 输入
for (int i = 0; i < n; i++) {
scanf("%d", &list[i]);
}
// 排序
sort(list, list + n);
// 计算
for (int i = 0; i < n; i++) {
if (list[i] <= mark) {
ans++;
mark = 1;
} else {
// 剪枝
int buffer = list[i] - mark - 1;
mark = list[i];
i += buffer;
}
}
printf("%lld\n", ans);
}
}
联系邮箱:curren_wong@163.com
CSDN:https://me.csdn.net/qq_41729780
知乎:https://zhuanlan.zhihu.com/c_1225417532351741952
公众号:复杂网络与机器学习
欢迎关注/转载,有问题欢迎通过邮箱交流。