Hackerrank--Team Formation
For an upcoming programming contest, Roy is forming some teams from the n students of his university. A team can have any number of contestants.
Roy knows the skill level of each contestant. To make the teams work as a unit, he should ensure that there is no skill gap between the contestants of the same team. In other words, if the skill level of a contestant is x, then he has either the lowest skill level in his team or there exists another contestant with skill level of x−1 in the same team. Also, no two contestants of the same team should have same skill level. Note that, some of the contestants always write buggy codes, their skill levels are negative.
It is clear that more the number of contestants in a team, more the problems they can attempt at a time. So Roy wants to form teams such that the size of the smallest possible team is maximized.
Input Format
The first line of input contains t (1≤t≤100), the number of test cases.
Each case contains an integer n (0≤n≤105), the number of contestants, followed by n space separated integers. The ith integer denotes the skill level of ith contestant. The absolute values of skill levels will not exceed 109.
The total number of contestants in all cases will not exceed 106.
Output Format
For each test case, print the output in a separate line.
Sample Input
4
7 4 5 2 3 -4 -3 -5
1 -4
4 3 2 3 1
7 1 -2 -3 -4 2 0 -1
Sample Output
3
1
1
7
Explanation
For the first case, Roy can form two teams: one with contestants with skill levels
{-4,-3,-5}
and the other one with{4,5,2,3}
. The first group containing3
members is the smallest.In the second case, the only team is
{-4}
In the third case, the teams are
{3}
,{1,2,3}
, the size of the smaller group being1
.In the last case, you can build a group containing all the contestants. The size of the group equals the total number of contestants.
思路:贪心。尽量减少分组的个数。
如果存在以当前这个数-1为结尾的组,那么就找到个数最少的一个将当前这个数加在后面,否则增加一个组,以当前数为结尾。
Accepted Code:
- using namespace std;
- #include <bits/stdc++.h>
- #define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0))
- #define rep(i,n) for(auto i=0; i<(n); i++)
- #define mem(x,val) memset((x),(val),sizeof(x));
- #define rite(x) freopen(x,"w",stdout);
- #define read(x) freopen(x,"r",stdin);
- #define all(x) x.begin(),x.end()
- #define sz(x) ((int)x.size())
- #define sqr(x) ((x)*(x))
- #define pb push_back
- #define clr clear()
- #define inf (1<<28)
- #define ins insert
- #define xx first
- #define yy second
- #define eps 1e-9
- int main(void) {
- ios_base::sync_with_stdio();
- int test;
- cin >> test;
- while ( test-- ) {
- map<int, priority_queue<int, vector<int>, greater<int> > > val;
- int n;
- cin >> n;
- vector<int> vec(n);
- rep(i, n) {
- cin >> vec[i];
- }
- sort( vec.begin(), vec.end() );
- rep(i, n) {
- int tmp = vec[i];
- int now = ;
- auto it = val.find(tmp - );
- if (it != val.end() && it->yy.size()) {
- now = it->yy.top();
- it->yy.pop();
- }
- now++;
- val[tmp].push(now);
- }
- int ans = INT_MAX;
- for ( auto x : val ) if ( x.second.size() )
- ans = min( ans, x.second.top() );
- if (ans == INT_MAX) ans = ;
- cout << ans << endl;
- }
- return ;
- }
Hackerrank--Team Formation的更多相关文章
- 位运算 ZOJ 3870 Team Formation
题目传送门 /* 题意:找出符合 A^B > max (A, B) 的组数: 位运算:异或的性质,1^1=0, 1^0=1, 0^1=1, 0^0=0:与的性质:1^1=1, 1^0=0, 0^ ...
- zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation
http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...
- 第十二届浙江省大学生程序设计大赛-Team Formation 分类: 比赛 2015-06-26 14:22 50人阅读 评论(0) 收藏
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- ZOJ3870 Team Formation
/** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...
- Team Formation(思维)
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- 2015 浙江省赛B Team Formation (技巧,动归)
Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...
- Zoj 3870——Team Formation——————【技巧,规律】
Team Formation Time Limit: 3 Seconds Memory Limit: 131072 KB For an upcoming programming contes ...
- ZOJ 3870 Team Formation 贪心二进制
B - Team Formation Description For an upcoming progr ...
- ZOJ 3870:Team Formation(位运算&思维)
Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...
- 浙江省第十二届省赛 B - Team Formation
Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...
随机推荐
- [Ceoi2016|BZOJ4936] Match
哈希+分治+stack 题目: 给你一个由小写字母组成的字符串s,要你构造一个字典序最小的(认为左括号的字典序比右括号小)合法的括号 序列与这个字符串匹配,字符串和括号序列匹配定义为:首先长度必须相等 ...
- 在vue项目中使用Nprogress.js进度条
NProgress是一款在网页顶部添加细长进度条的工具,非常轻巧,使用起来也非常便捷,灵感来源于Google, YouTube. 1.安装 $ npm install --save nprogress ...
- 快速沃尔什变换(FWT) 与 快速莫比乌斯变换 与 快速沃尔什变换公式推导
后面的图片将会告诉: 如何推出FWT的公式tf 如何推出FWT的逆公式utf 用的是设系数,求系数的方法! ============================================== ...
- Ubuntu 18.04 Linux上安装Etherpad,基于Web的实时协作编辑器
介绍 Etherpad是一个开源的,基于Web的实时协作编辑器(http://www.0834nanke.com) 它允许多个人使用他们的Web浏览器同时编辑文档. 它还提供了一些很酷的功能,如富文本 ...
- Apache添加多端口及实现单ip多端口映射的方法
这篇文章主要介绍了Apache添加多端口及实现单ip多端口映射的方法的相关资料,需要的朋友可以参考下(http://www.0831jl.com) 先给大家说下apache添加多端口的方法,具体步骤如 ...
- Ubuntu GitHub操作——分支、合并与标签
分支 分支是用来将特性开发绝缘开来的.在你创建仓库的时候,master 是"默认的"分支.在其他分支上进行开发,完成后再将它们合并到主分支上. 创建一个叫做"featur ...
- IOS6 新特性之UIRefreshControl
"不会模仿的公司不是好公司不会剽窃的公司不是优秀公司 不会调戏代码的不是骨灰级码工 你同意吗? 苹果估计想取代第三方的pull to refresh" ------ ...
- 图书-技术-SpringBoot:《Spring Boot2 + Thymeleaf 企业应用实战》
ylbtech-图书-技术-SpringBoot:<Spring Boot2 + Thymeleaf 企业应用实战> <Spring Boot 2+Thymeleaf企业应用实战&g ...
- C++写矩阵的转置
(2019年2月19日注:这篇文章原先发在自己github那边的博客,时间是2017年2月5日) 对于任意非n阶矩阵的转置,用c++应该怎么写代码,思考了一下,发现并没有那么简单,上网找到了一个比较好 ...
- 版本控制git之二 分支 切换分支 创建分支 合并 删除
版本控制git之二 分支 有人把 Git 的分支模型称为它的`‘必杀技特性’',也正因为这一特性,使得 Git 从众多版本控制系统中脱颖而出. 为何 Git 的分支模型如此出众呢? Git 处 ...