ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【CodeForces 1249A --- Yet Another Dividing into Teams】

2019-10-29 14:03:18  阅读:857  来源: 互联网

标签:Dividing students into CodeForces number int ai team query


【CodeForces 1249A --- Yet Another Dividing into Teams】

Description

You are a coach of a group consisting of n students. The i-th student has programming skill ai. All students have distinct programming skills. You want to divide them into teams in such a way that:

No two students i and j such that |ai−aj|=1 belong to the same team (i.e. skills of each pair of students in the same team have the difference strictly greater than 1);
the number of teams is the minimum possible.
You have to answer q independent queries.

Input

The first line of the input contains one integer q (1≤q≤100) — the number of queries. Then q queries follow.

The first line of the query contains one integer n (1≤n≤100) — the number of students in the query. The second line of the query contains n integers a1,a2,…,an (1≤ai≤100, all ai are distinct), where ai is the programming skill of the i-th student.

Output

For each query, print the answer on it — the minimum number of teams you can form if no two students i and j such that |ai−aj|=1 may belong to the same team (i.e. skills of each pair of students in the same team has the difference strictly greater than 1)

Sample Input

4
4
2 10 1 20
2
3 6
5
2 3 4 99 100
1
42

Sample Output

2
1
2
1

解题思路:题目要求同一分组内任意两个数的差值不等于1,仔细相一下,那么只要相邻的数不在同一个的分组内,这样一想,只要存在一对相邻的数那么就有两个分组,那么其实也就需要两个分组就足够了,其他数字分别进入这两个分组就好。为了减小时间复杂度,先将所有数字快排一遍,在遇到第一对相邻的数差值=1时,跳出循环。

 

 

AC代码:

#include <iostream>
#include <algorithm>
using namespace std;
int a[120];
int main()
{
    int q,n;
    int flag=0;
    while(cin>>q)
    {
        for(int i=0;i<q;i++)
        {
            cin>>n;
            flag=0;
            for(int j=0;j<n;j++)
                cin>>a[j];
            sort(a,a+n);
            for(int k=0;k<n-1;k++)
            {
                if(a[k+1]-a[k]==1)
                {
                    flag=1;
                    break;
                }
            }
            if(flag==0)
                cout<<1<<endl;
            else
                cout<<2<<endl;
        }
        
    }
    return 0;
} 

 

标签:Dividing,students,into,CodeForces,number,int,ai,team,query
来源: https://www.cnblogs.com/lovelcy/p/11758312.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有