CodeForces 1249A --- Yet Another Dividing into Teams
【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的更多相关文章
- 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 ...
- 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 ...
- Codeforces 380E Sereja and Dividing
题面 洛谷传送门 题解 博客 有精度要求所以只用求几十次就差不多了 CODE #include <bits/stdc++.h> using namespace std; typedef l ...
- Codeforces Round #595 (Div. 3)
A - Yet Another Dividing into Teams 题意:n个不同数,分尽可能少的组,要求组内没有两个人的差恰为1. 题解:奇偶分组. int a[200005]; void te ...
- CF-595
题目传送门 A .Yet Another Dividing into Teams sol:原先是用比较复杂的方法来解的,后来学弟看了一眼,发现不是1就是2,当出现两个人水平相差为1就分成两组,1组全是 ...
- 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 ...
- codeforces 478B Random Teams
codeforces 478B Random Teams 解题报告 题目链接:cm.hust.edu.cn/vjudge/contest/view.action?cid=88890#probl ...
- 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 ...
- Codeforces 552 E. Two Teams
E. Two Teams time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- mysql中source提高导入数据速率的方法
示例: 第一步: 第二步: 使用 source 导入你所需要导入的文件 第三步: 在导入的数据停止后,输入 commit; 这样数据就算是导入完成了.
- Java-CharTools工具类
package com.gootrip.util; import java.io.UnsupportedEncodingException; /** * <p>Title:字符编码工具类 ...
- javascript中的正确错误处理------------引用
JavaScript的事件驱动机制让JavaScript更加丰富,浏览器好比就是一个事件驱动的机器,错误也是一种事件.当一个错误发生时,一个事件就在某个点抛出. 解释起来就是,当发生错误时,JavaS ...
- 利用msyqlfont + plsql 客户端 完成msyql数据向oracle的转移
方法一: 1.这是mysqlfont 连接工具 ,选中表右键点击 输出->csv文件 2.选择导出的文件为ANSI型,因为csv文件excel打开的默认编码方式为ANSI这样可以防止中文在exc ...
- 十进制数转N进制c++实现
编写一个算法,将一个非负的十进制整数N转换为另一个基数为B的B进制整数. #include <iostream> #include<string.h> using namesp ...
- codevs3269 混合背包 x
3269 混合背包 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题目描述 Description 背包体积为V ,给出N个物品,每个物品占用体积为 ...
- 顺序表应用3:元素位置互换之移位算法(SDUT 3326)
题解:用一个for,循环m次,每次都把最前面的放到最后面,就可以了. #include <stdio.h> #include <stdlib.h> #include <s ...
- BeetleX之快速构建Web多房间聊天室
其实构建一个Web多房间聊天室也并不是什么困难的技术,借助于websocket就可以轻松实现多用户在线实时通讯交互:在这里主要介绍一下在BeetleX和BeetleXjs的支持下如何让这个功能实现的更 ...
- Java线程之wait()、notify()、notifyAll()
翻译:https://www.journaldev.com/1037/java-thread-wait-notify-and-notifyall-example 简述 java中Objct对象包含三个 ...
- JavaWeb_(Hibernate框架)Hibernate与c3p0与Dbutils的区别
JavaWeb_(Hibernate框架)使用Hibernate开发用户注册功能 传送门 JavaWeb_(Hibernate框架)使用c3p0与Dbutils开发用户注册功能 传送门 Hiberna ...