Duizi and Shunzi

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2883    Accepted Submission(s): 1110

Problem Description
Nike likes playing cards and makes a problem of it.

Now give you n integers, ai(1≤i≤n)

We define two identical numbers (eg: 2,2) a Duizi,
and three consecutive positive integers (eg: 2,3,4) a Shunzi.

Now you want to use these integers to form Shunzi and Duizi as many as possible.

Let s be the total number of the Shunzi and the Duizi you formed.

Try to calculate max(s).

Each number can be used only once.

 
Input
The input contains several test cases.

For each test case, the first line contains one integer n(1≤n≤106). 
Then the next line contains n space-separated integers ai (1≤ai≤n)

 
Output
For each test case, output the answer in a line.
 
Sample Input
7
1 2 3 4 5 6 7
9
1 1 1 2 2 2 3 3 3
6
2 2 3 3 3 3
6
1 2 3 3 4 5
 
Sample Output
2
4
3
2
Hint

Case 1(1,2,3)(4,5,6)

Case 2(1,2,3)(1,1)(2,2)(3,3)
Case 3(2,2)(3,3)(3,3)
Case 4(1,2,3)(3,4,5)
 
Source
 
 
题意:输入一个n,然后输入n张牌,每张牌都不大于n,问可以组成最多的对子数和顺子数,对子是两个相同的牌,顺子是连续的三张牌
思路:从1到n遍历, 如果第 i 张牌能打完对子还有多一张,那么看 i+1 张打完对子是不是可以多,如果多的话只要 i+2 有这张牌就可以打出一个顺子,在计算第 i 张牌的时候记得把对子的数量加进去,在可以组成顺子的时候记得把 第 i+1 和 i +2 凑顺子的那一张牌减掉。
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=1e6+5;
int a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
int t;
for(int i=0;i<n;i++)
{
scanf("%d",&t);
a[t]++;
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=a[i]/2;
if(i<=n-2 && a[i]%2 && a[i+1]%2 && a[i+2])
{
ans++;
a[i]--;
a[i+1]--;
a[i+2]--;
}
}
printf("%d\n",ans);
}
}

Duizi and Shunzi HDU的更多相关文章

  1. Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. 【hdu6188】Duizi and Shunzi(贪心)

    2017ACM/ICPC广西邀请赛 重现赛1007 Duizi and Shunzi 题意 有n张牌,问你最多能组成多少对子+顺子?一个牌只能用在一个顺子或者对子中. 题解 本来想写dp的,不会..小 ...

  3. 2017ACM/ICPC广西邀请赛 1007 Duizi and Shunzi

    Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...

  5. HDU 6188 Duizi and Shunzi 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续 ...

  6. HDU 6188 Duizi and Shunzi

    栈. 将数字排序后,一个一个压入栈.如果栈顶两个元素形成了对子,那么$ans+1$,弹出栈顶两个元素:如果栈顶三个元素形成了顺子,那么$ans+1$,弹出栈顶三个元素. #include<bit ...

  7. 2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi

    Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...

  8. hdu6188 Duizi and Shunzi (贪心或者dp)

    题意 有n张牌,第i张牌上的数字是a[i].我们定义 两张数字是一样的牌 为对子.我们定义 三张数字连续的牌 为顺子.我们想把这n张牌组成尽可能多的顺子和对子.请计算并输出能组成的最多的顺子和对子的数 ...

  9. 2017ACM/ICPC广西邀请赛 Duizi and Shunzi

    题意:就是一个集合分开,有两种区分 对子:两个相同数字,顺子:连续三个不同数字,问最多分多少个 解法:贪心,如果当前数字不构成顺子就取对子 /2,如果可以取顺子,那么先取顺子再取对子 #include ...

随机推荐

  1. pytest之assert断言

    assert pytest允许您使用标准Python断言来验证Python测试中的期望和值.例如,你可以写下 # content of test_assert1.py def f(): return ...

  2. window.location.herf传值问题

    各个值之间用&&&&&&连接 新版本的tomcat不支持其他字符,需要通过encodeURIComponent编码 变量名数字后不能直接加字母 such ...

  3. lampp ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'lepus'

    解决方法: 在[mysqlld]段下增加如下代码:skip-grant-tables: 1.which mysql 查看mysql位置,例如:/opt/lampp/bin/mysql 2.进入配置my ...

  4. phpstorm更换主题

    打开PhpStorm,File -- Settings -- Editor -- Color Scheme --General选择你喜欢的风格进行更改,选择完成后单击Apply

  5. 【转】Java Future 怎么用 才算是真正异步

    接着上一篇继续并发包的学习,本篇说明的是Callable和Future,它俩很有意思的,一个产生结果,一个拿到结果.        Callable接口类似于Runnable,从名字就可以看出来了,但 ...

  6. OpenTLD相关资料

    这是一位来自奥地利的博士生的博客 他的介绍如下: I am a PhD student at the Safety and Security Department of the Austrian In ...

  7. linux中mysql安装(配图)

    环境: 1.操作系统:CentOS release 6.8 (Final) 2.安装版本: mysql-5.6.31-linux-glibc2.5-x86_64.tar.gz 3.下载地址:http: ...

  8. 部署prerender服务器

    // 安装 git sudo apt-get install git sudo apt-get install curl // 请先确认服务器是否安装了curl 如果已经安装跳过即可 // 安装 no ...

  9. python collections 模块 常用集合

    1.nametulpe # tuple 可以表示不变集合 列如坐标 point = (1,1) # 缺点是 只可以通过下标方式访问 #namedtuple是一个函数,它用来创建一个自定义的tuple对 ...

  10. WPF学习笔记五之MVVM

    1.概念 MVVM是Model-View-ViewModel的缩写形式,它通常被用于WPF或Silverlight开发.这三者之间的关系如下图所示 模型(Model) Model——可以理解为带有字段 ...