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. NumPy简介

    NumPy是什么? NumPy(Numerrical Python 的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然的使用数组.NumPy包含很多实用的数学函数,涵盖线性代数运 ...

  2. web信息泄露注意事项

    1. 确保您的Web服务器不发送显示有关后端技术类型或版本信息的响应头. 2. 确保服务器打开的端口上运行的所有服务都不会显示有关其构建和版本的信息. 3. 确保所有目录的访问权限正确,保证不会让攻击 ...

  3. 深度学习之 GAN 进行 mnist 图片的生成

    深度学习之 GAN 进行 mnist 图片的生成 mport numpy as np import os import codecs import torch from PIL import Imag ...

  4. Mock API是如何在开发中发光发热的?

    在长期的服务过程中,我们经常会遇到前来咨询的用户与我们反馈以下这种情况:咨询者是一个前端人员,在项目开发的过程中需要与后端进行对接,遇到后端还没完成数据输出的情况下,他只好写静态模拟数据,在遇到大型项 ...

  5. Spring Security 入门(1-6-1)Spring Security - 配置文件解析和访问请求处理

    1.在pom.xml中添加maven坐标 <dependency> <groupId>org.springframework.security</groupId> ...

  6. Windows10+Docker搭建分布式Redis集群(一)

    摘要,Docker for Windows 仅支持专业版 目录 第一步:检查系统支持虚拟化 第二步:下载Docker对应版本 第三步:配置镜像加速 第一步:检查系统是否支持虚拟化 Docker前提是需 ...

  7. python CSS

    CSS 一. css的四种引入方式   1.行内式  2.嵌入式  3. 链接式 将一个.css文件引入到HTML文件中 1 <link href="mystyle.css" ...

  8. transform-style为什么子元素需要定位?

    有个园友问我一个问题: 为什么ul和li都要absolute定位呢,让其自然排列,然后沿着x轴进行旋转不行吗?这块一直无法理解. 在这里进行详细的解答: 我们知道圆是有圆心和半径的, 我用定位的方式就 ...

  9. 南京邮电大学java第一次实验报告

    实 验 报 告 ( 2017 / 2018学年 第2学期) 课程名称 JAVA语言程序设计 实验名称 Java集成开发环境的安装与使用. Java变量.表达式与控制结构 实验时间 2018 年 4 月 ...

  10. C++ namespace的作用

    namespace:命名空间或者叫名字空间,传统的c++只有一个全局的namespace,但是由于现在的程序规模越来越大,程序的分工越来越细,全局作用域就变得越来越拥挤,每个人都可能使用相同的名字来实 ...