【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[];
int main()
{
int q,n;
int flag=;
while(cin>>q)
{
for(int i=;i<q;i++)
{
cin>>n;
flag=;
for(int j=;j<n;j++)
cin>>a[j];
sort(a,a+n);
for(int k=;k<n-;k++)
{
if(a[k+]-a[k]==)
{
flag=;
break;
}
}
if(flag==)
cout<<<<endl;
else
cout<<<<endl;
} }
return ;
}

CodeForces 1249A --- Yet Another Dividing into Teams的更多相关文章

  1. Codeforces Round #443 (Div. 1) B. Teams Formation

    B. Teams Formation link http://codeforces.com/contest/878/problem/B describe This time the Berland T ...

  2. codeforces 632B B. Alice, Bob, Two Teams(暴力)

    B. Alice, Bob, Two Teams time limit per test 1.5 seconds memory limit per test 256 megabytes input s ...

  3. Codeforces 380E Sereja and Dividing

    题面 洛谷传送门 题解 博客 有精度要求所以只用求几十次就差不多了 CODE #include <bits/stdc++.h> using namespace std; typedef l ...

  4. Codeforces Round #595 (Div. 3)

    A - Yet Another Dividing into Teams 题意:n个不同数,分尽可能少的组,要求组内没有两个人的差恰为1. 题解:奇偶分组. int a[200005]; void te ...

  5. CF-595

    题目传送门 A .Yet Another Dividing into Teams sol:原先是用比较复杂的方法来解的,后来学弟看了一眼,发现不是1就是2,当出现两个人水平相差为1就分成两组,1组全是 ...

  6. Codeforces Round #452 (Div. 2)-899A.Splitting in Teams 899B.Months and Years 899C.Dividing the numbers(规律题)

    A. Splitting in Teams time limit per test 1 second memory limit per test 256 megabytes input standar ...

  7. codeforces 478B Random Teams

    codeforces   478B  Random Teams  解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...

  8. Codeforces Round #360 (Div. 1) D. Dividing Kingdom II 暴力并查集

    D. Dividing Kingdom II 题目连接: http://www.codeforces.com/contest/687/problem/D Description Long time a ...

  9. Codeforces 552 E. Two Teams

    E. Two Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

随机推荐

  1. php类相关知识---__unset和__isset

    __unset 删除非公有属性,在外部调用unset时发生,  __isset用来检测对象属性是否设置值 <?php class coach { protected $chairfit = &q ...

  2. cnblogs设置各级标题样式和目录

    向博客园申请js权限 我们需要进入博客园自定义博客模板的页面,向博客园管理团队申请页面运行js的权限. [博客园]->[设置]->[博客设置],点击页面上的js权限申请,然后填写申请的理由 ...

  3. SpringMVC全局异常统一处理

    SpringMVC全局异常统一处理以及处理顺序最近在使用SpringMVC做全局异常统一处理的时候遇到的问题,就是想把ajax请求和普通的网页请求分开返回json错误信息或者跳转到错误页. 在实际做的 ...

  4. js比较两个时间的大小

    function checkdate(s,e){ //得到日期值并转化成日期格式,replace(/-/g, "//")是根据验证表达式把日期转化成长日期格式,这样再进行判断就好判 ...

  5. [python](Docker SDK)上传镜像到私有仓库(tls、身份认证)

    (Docker SDK)上传镜像到私有仓库(tls.身份认证) API:https://docker-py.readthedocs.io/en/stable/ 环境:python:3.7.3 配置参数 ...

  6. C库函数-fgets()

    函数声明:char *fgets(char *str,int n,FILE *stream) 函数介绍:从指定的stream流中读取一行,并把它存储在str所指向的字符串中.当读取到(n-1)个字符时 ...

  7. webuploader的一些体验

    WebUploader是由Baidu WebFE(FEX)团队开发的一个简单的以HTML5为主,FLASH为辅的现代文件上传组件.支持大文件分片并发上传. 具体api文档参考:http://fex.b ...

  8. python进程间的通信

    from multiprocessing import Queue, Process import time, random # 要写入的数据 list1 = ["java", & ...

  9. flask 第五篇

    需求: 1. 用户名: oldboy 密码: oldboy123 2. 用户登录成功之后跳转到列表页面 3. 失败有消息提示,重新登录 4.点击学生名称之后,可以看到学生的详细信息 后端: from ...

  10. C++入门经典-例8.7-多态,利用虚函数实现动态绑定

    1:多态性是面向对象程序设计的一个重要特征,利用多态性可以设计和实现一个易于扩展的系统.在C++语言中,多态是指具有不同功能的函数可以用同一个函数名,这样就可以用一个函数名调用不同内容的函数,发出同样 ...