【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. 数据库中char和varchar的区别

    1. char类型的长度是固定的,varchar的长度是可变的. 这就表示,存储字符串'abc',使用char(10),表示存储的字符将占10个字节(包括7个空字符) 使用varchar2(10),, ...

  2. MQTT 入门介绍——菜鸟教程

    一.简述 MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级& ...

  3. 51nod 1172 Partial Sums V2

    题目 给出一个数组A,经过一次处理,生成一个数组S,数组S中的每个值相当于数组A的累加,比如:A = {1 3 5 6} => S = {1 4 9 15}.如果对生成的数组S再进行一次累加操作 ...

  4. 【Winform-自定义控件】ImageButton 支持鼠标正常、悬停、按下更改图片,支持文本

    原文地址:https://www.codeproject.com/Articles/29010/WinForm-ImageButton 自定义winfrom图片按钮:支持鼠标正常.悬停.按下更改图片, ...

  5. http 异步 接收 回传 数据文字和文件流

    public void HttpListenerStar() { try { HttpListener httpListener = new HttpListener(); httpListener. ...

  6. vue中改变数组或对象,页面没做出对应的渲染

    原文链接 数组更新检测 变异方法 Vue 包含一组观察数组的变异方法,所以它们也将会触发视图更新.这些方法如下: push() pop() shift() unshift() splice() sor ...

  7. sql 语句中 order by 的用法

    order by 是用在where条件之后,用来对查询结果进行排序 order by 字段名 asc/desc asc 表示升序(默认为asc,可以省略) desc表示降序 order by 无法用于 ...

  8. AcWing:148. 合并果子(哈夫曼树)

    在一个果园里,达达已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆. 达达决定把所有的果子合成一堆. 每一次合并,达达可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和. 可以看出 ...

  9. Vue 新手学习笔记:vue-element-admin 之安装,配置及入门开发

    所属专栏: Vue 开发学习进步 说实话都是逼出来的,对于前端没干过ES6都不会的人,vue视频也就看了基础的一些但没办法,接下来做微服务架构,前端就用 vue,这块你负责....说多了都是泪,脚手架 ...

  10. require.context

    带表达式的 require 语句 如果你的 require参数含有表达式(expressions),会创建一个上下文(context),因为在编译时(compile time)并不清楚具体是哪一个模块 ...