题目链接

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 containing 3 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 being 1.

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的更多相关文章

  1. 位运算 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^ ...

  2. zoj The 12th Zhejiang Provincial Collegiate Programming Contest Team Formation

    http://acm.zju.edu.cn/onlinejudge/showContestProblem.do?problemId=5494 The 12th Zhejiang Provincial ...

  3. 第十二届浙江省大学生程序设计大赛-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 ...

  4. ZOJ3870 Team Formation

    /** Author: Oliver ProblemId: ZOJ3870 Team Formation */ /* 思路 1.异或运算,使用^会爆,想到二进制: 2.我们可以试着从前往后模拟一位一位 ...

  5. Team Formation(思维)

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  6. 2015 浙江省赛B Team Formation (技巧,动归)

    Team Formation For an upcoming programming contest, Edward, the headmaster of Marjar University, is ...

  7. Zoj 3870——Team Formation——————【技巧,规律】

    Team Formation Time Limit: 3 Seconds      Memory Limit: 131072 KB For an upcoming programming contes ...

  8. ZOJ 3870 Team Formation 贪心二进制

                                                    B - Team Formation Description For an upcoming progr ...

  9. ZOJ 3870:Team Formation(位运算&思维)

    Team Formation Time Limit: 2 Seconds Memory Limit: 131072 KB For an upcoming programming contest, Ed ...

  10. 浙江省第十二届省赛 B - Team Formation

    Description For an upcoming programming contest, Edward, the headmaster of Marjar University, is for ...

随机推荐

  1. 【珍惜时间】iReport

    项目很点意思,感觉很高超的样子 先放下项目的github地址:https://github.com/tctangyanan/iReport 感谢各位伟大的程序员无私的分享自己的技术 老规矩,我们会运行 ...

  2. Math: Fibonacci

    https://www.zhihu.com/question/28062458 http://blog.csdn.net/hikean/article/details/9749391 对于Fibona ...

  3. 2016.10.29初中部上午NOIP普及组比赛总结

    2016.10.29[初中部 NOIP普及组 ]模拟赛 做得好爽! 进度: 比赛:35+45+AC+0=180 改题:AC+AC+AC+0=300 幸运的数 有点无语--之前怕超限,还特意利用程序打了 ...

  4. 阿里巴巴大数据产品最新特性介绍--机器学习PAI

    以下内容根据演讲视频以及PPT整理而成. 本次分享主要围绕以下五个方面: PAI产品简介 自定义算法上传 数加智能生态市场 AutoML2.0自动调参 AutoLearning自动学习 一.PAI产品 ...

  5. Quartz:Quartz

    ylbtech-Quartz:Quartz Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Qu ...

  6. 博弈论 | 暑期集训Day2学习总结

    今天的知识点为博弈论. 相比于昨天完全陌生难懂的概念,今天接触到的东西应该算是非常容易理解了,一下子又对ACM的学习重拾信心.毕竟game作为主题也吸引眼球,每种博弈背景下引入的游戏介绍也十分有趣.主 ...

  7. 第一周课堂笔记5th

    1.      While循环机制 如何终止循环? 改变条件(根据上面的流程,只要改变条件,就会终止循环). 关键字:break. 调用系统命令:quit(),exit() 后面会讲到,不建议大家使用 ...

  8. STL容器-deque-双端队列

    注明:全部来自转载,供自己学习与复习使用 deque双向开口可进可出的容器 我们知道连续内存的容器不能随意扩充,因为这样容易扩充别人那去 deque却可以,它创造了内存连续的假象. 其实deque由一 ...

  9. Windows API 第四篇 文件操作

    创建或打开文件(也可用于打开管道,油槽,硬件设备等): HANDLE CreateFile( LPCTSTR lpFileName, // file name DWORD dwDesiredAcces ...

  10. 枚举进程,线程,堆 CreateToolhelp32Snapshot

    Takes a snapshot of the processes and the heaps, modules, and threads used by the processes.对当前系统进行一 ...