Codeforces Round #643 (Div. 2) B. Young Explorers(贪心)
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.
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 .
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.
2 3 1 1 1 5 2 3 1 2 2
3 2
首先把数组从小到大sort一遍,然后从头开始遍历。
每到一个数先++cnt,如果这时cnt==当前的a[i]说明能凑成一组了,直接ans++,同时cnt=0置零。
贪心策略就是用尽可能少的数凑出来一组,因此要从小到大遍历。
比如 1 1 3,从小到大能凑出两组:1和1,从大到小只能凑出一组3 1 1。
#include <bits/stdc++.h> using namespace std; int n,a[200005]; int main() { int t; cin>>t; while(t--) { cin>>n; int i; for(i=1;i<=n;i++)scanf("%d",&a[i]); sort(a+1,a+n+1); int ans=0; int cnt=0; a[n+1]=0x3f3f3f3f; for(i=1;i<=n+1;i++) { cnt++; if(a[i]==cnt) { ans++; cnt=0; } } cout<<ans<<endl; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!