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. Linux进程调度分析

    原文:http://www.2cto.com/os/201112/113229.html 操作系统要实现多进程,进程调度必不可少. 有人说,进程调度是操作系统中最为重要的一个部分.我觉得这种说法说得太 ...

  2. Flask 页面缓存逻辑,jinja2 过滤器,测试器

    回调接入点-页面缓存逻辑 from flask import Flask,request,render_template from werkzeug.contrib.cache import Simp ...

  3. asp.net web api 控制器

    1控制器操作的参数 控制器操作的参数可以是内置类型也可以是自定义类型,无参也是允许的. 2控制器操作返回值 类型 说明 void 操作返回值为void时,Web API返回空HTTP响应,其状态码为2 ...

  4. vue初尝试--新建项目

    这是一篇技术贴--如何新建一个基于vue的项目 1.下载对应版本的nodejs安装,下载的nodejs都集成了npm,所以nodejs安装完成之后npm也对应安装完成了. 安装完成之后可以在cmd命令 ...

  5. c#动态加载卸载DLL

    前段时间工作的时候遇到一个问题.就是需要每次启动程序的时候动态替换掉某个dll,所以就百度了这方面的资料.这次记录下来让自己以后可以看. 根据自己的理解,动态卸载dll需要有以下条件: 1:dll在加 ...

  6. MVC Form 表单 提交 集合 及 复杂对象

    public class Customer { public string FName{get;set;} public Address address{get;set;} } public clas ...

  7. angular2 学习笔记 ( unit test 单元测试 )

    第一次写单元测试. 以前一直都有听说 TDD 的事情. 今天总算是去尝试了一下. 先说说 TDD 的想法, 是这样的, 开发项目的流程 : 确定需求 -> 写类,接口,方法的名字(不写具体实现代 ...

  8. 2.x与3.x差异、条件语句、数据类型、其他

    一.输入(raw_input)=====>python2.x版本 #!/usr/bin/env python # -*- coding: utf-8 -*- # 将用户输入的内容赋值给 name ...

  9. PyMySQL模块的使用

    PyMySQL介绍 PyMySQL是在Python3.x版本中用于连接MySQL服务器的一个库,Python2系列中则使用mysqldb.Django中也可以使用PyMySQL连接MySQL数据库. ...

  10. [NOIP2009][LuoguP1073] 最优贸易 - Tarjan,拓扑+DP

    Description&Data 题面:https://www.luogu.org/problemnew/show/P1073 Solution Tarjan对联通块缩点,在DAG上按照拓扑序 ...