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)

题目大意:有两种操作:可以用三张连续的牌组成顺子,可以用两张相同的牌组成对子,记对子和顺子的和为S,求Max(s)

解题报告:这题考场没写出来,第一步很容易想到,那就是把多出来的对子先打掉,这样显然更优(虽然此时可以组成顺子,但是组成对子是等价的,不会影响答案),现在每一张牌就只剩下1或2张了,考虑剩下的牌怎么打..

然后我就挂在了这里,天真的以为直接能打顺子就打掉,打完即可,然后挂在了手make的1 1 2 3 3 这组上,此时考试已经结束了..考后发现这里是需要DP的,对于剩一张牌的我们不需要决策,能用到就用到.剩两张牌需要做决策,显然是可以选择打一个对子或两个顺子,显然两个顺子是更好的,所以能打两个顺子就打掉,不能就打对子.

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int N=1e6+5;
int t[N],n,f[N];
void work()
{
int x,m=0,ans=0;
for(RG int i=0;i<=n;i++)f[i]=0,t[i]=0;
for(int i=1;i<=n;i++){
scanf("%d",&x);
t[x]++;m=Max(x,m);
}
for(int i=1;i<=m;i++){
if(t[i]>2){
ans+=t[i]>>1;
if(t[i]%2==0)ans--;
if(t[i]%2)t[i]=1;
else t[i]=2;
}
}
f[1]=t[1]>>1;if(t[1]>1)t[1]=0;
f[2]=f[1]+(t[2]>>1);if(t[2]>1)t[2]=0;
for(int i=3;i<=m;i++){
f[i]=f[i-1];
if(!t[i])continue;
if(t[i]==2)
f[i]=f[i-1]+1;
if(t[i-1] && t[i-2] && f[i-2]+1>=f[i]){
f[i]=f[i-2]+1;t[i]--;t[i-1]--;t[i-2]--;
}
else{
if((t[i-1] && t[i-2] && f[i]>f[i-2]+1) || !t[i-1] || !t[i-2]){
if(t[i]==2)t[i]=0;//如果不能打出两个顺子,那么直接打对子
}
}
}
printf("%d\n",ans+f[m]);
} int main()
{
while(~scanf("%d",&n))work();
return 0;
}

2017ACM/ICPC广西邀请赛-重现赛 1007.Duizi and Shunzi的更多相关文章

  1. 2017ACM/ICPC广西邀请赛-重现赛

    HDU 6188 Duizi and Shunzi 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 思路: 签到题,以前写的. 实现代码: #inc ...

  2. 2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)

    上一场CF打到心态爆炸,这几天也没啥想干的 A Math Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  3. 2017ACM/ICPC广西邀请赛-重现赛 1010.Query on A Tree

    Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learne ...

  4. 2017ACM/ICPC广西邀请赛-重现赛 1004.Covering

    Problem Description Bob's school has a big playground, boys and girls always play games here after s ...

  5. 2017ACM/ICPC广西邀请赛-重现赛 1001 A Math Problem

    2017-08-31 16:48:00 writer:pprp 这个题比较容易,我用的是快速幂 写了一次就过了 题目如下: A Math Problem Time Limit: 2000/1000 M ...

  6. 2017ACM/ICPC广西邀请赛-重现赛1005 CS course

    2017-08-31 16:19:30 writer:pprp 这道题快要卡死我了,队友已经告诉我思路了,但是做题速度很缓慢,很费力,想必是因为之前 的训练都是面向题解编程的缘故吧,以后不能这样了,另 ...

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

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

  8. 2017ACM/ICPC广西邀请赛

    A.A Math Problem #include <bits/stdc++.h> using namespace std; typedef long long ll; inline ll ...

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

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

随机推荐

  1. 团队作业4——第一次项目冲刺(Alpha版本)2017.11.16

    第一次会议:2017-11-16 大家的任务完成的不错^_^,继续努力了. 上图: 忘记照了,额....... 会议主要内容: 1.登录功能的讨论 2. 代码统一 具体分工: 成员 计划任务 遇见难题 ...

  2. jsp文件调用本地文件的方法(Tomcat server.xml 设置虚拟目录)

    JSP文件: <video id="my-video" class="video-js" controls preload="auto" ...

  3. React 深入系列1:React 中的元素、组件、实例和节点

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列,深入讲解了React中的重点概念.特性和模式等,旨在帮助大家加深对React的理解,以及在项目中 ...

  4. eclipse maven项目目录

    今天遇见一个错误,关于eclipse项目的路径问题,web-inf的路径,上图和下图出现了两种web-INF,src的web-INFf和webContent的web-INF,src里面的文件需要编译以 ...

  5. Web Api 过滤器之 AuthorizationFilter 验证过滤器

    该过滤器是最先执行的过滤器,即使把它放在最后 API [MyActionFilter] [MyExceptionFilter] [MyAuthorize] public void Get() { Tr ...

  6. Python内置函数(31)——object

    英文文档: class objectReturn a new featureless object. object is a base for all classes. It has the meth ...

  7. Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1

    摘要: Step by Step 真正从零开始,TensorFlow详细安装入门图文教程!帮你完成那个最难的从0到1 安装遇到问题请文末留言. 悦动智能公众号:aibbtcom AI这个概念好像突然就 ...

  8. Linux下的Shell编程(2)环境变量和局部变量

    Shell Script是一种弱类型语言,使用变量的时候无需首先声明其类型. 局部变量在本地数据区分配内存进行存储,这个变量归当前的Shell所有,任何子进 程都不能访问本地变量.这些变量与环境变量不 ...

  9. Docker学习笔记 - Docker容器的网络基础

    一.虚拟网桥 docker0 docker0 是 linux的虚拟网桥,守护进程通过docker0给容器提供网络连接的各种服务. 网桥是数据链路层设备,通常ip地址是网络层的设置.linux的虚拟网桥 ...

  10. jquery下关于input和label的关于点击事件的坑

    待填坑: 法院费用结算页面的案件类型