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 x1 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 (1t100), the number of test cases.

Each case contains an integer n (0n105), 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:

 1 using namespace std;  2 #include <bits/stdc++.h>

 3 

 4 #define sgn(x,y) ((x)+eps<(y)?-1:((x)>eps+(y)?1:0))

 5 #define rep(i,n) for(auto i=0; i<(n); i++)

 6 #define mem(x,val) memset((x),(val),sizeof(x));

 7 #define rite(x) freopen(x,"w",stdout);

 8 #define read(x) freopen(x,"r",stdin);

 9 #define all(x) x.begin(),x.end()

10 #define sz(x) ((int)x.size())

11 #define sqr(x) ((x)*(x))

12 #define pb push_back

13 #define clr clear()

14 #define inf (1<<28)

15 #define ins insert

16 #define xx first

17 #define yy second

18 #define eps 1e-9

19 

20 int main(void) { 21     ios_base::sync_with_stdio(0); 22     int test; 23     cin >> test; 24     while ( test-- ) { 25         map<int, priority_queue<int, vector<int>, greater<int> > > val; 26         int n; 27         cin >> n; 28         vector<int> vec(n); 29  rep(i, n) { 30             cin >> vec[i]; 31  } 32  sort( vec.begin(), vec.end() ); 33         

34  rep(i, n) { 35             int tmp = vec[i]; 36             int now = 0; 37             auto it = val.find(tmp - 1); 38             if (it != val.end() && it->yy.size()) { 39                 now = it->yy.top(); 40                 it->yy.pop(); 41  } 42             now++; 43  val[tmp].push(now); 44  } 45         int ans = INT_MAX; 46         for ( auto x : val ) if ( x.second.size() ) 47             ans = min( ans, x.second.top() ); 48         if (ans == INT_MAX) ans = 0; 49         cout << ans << endl; 50  } 51     

52     return 0; 53 }

 

你可能感兴趣的:(format)