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. Mego(07) - 关系配置

    这个是本框架的重要功能,该关系就是指对象中的复杂对象或集合属性,该关系与EF中的关系是有区别的.EF中强调关系的成对出现,这是由于数据库关系的思想决定的.然而Mego更接近与对象化逻辑,我们只关心当前 ...

  2. BlueMix - IBM的Paas云计算平台

    Bluemix,2015年年中,IBM推出了名为Bluemix的云计算平台.这一"平台即服务"的PaaS云将帮助开发者更快的进行应用开发和部署.   Bluemix正是IBM回应这 ...

  3. django中图片的上传和显示

    上传图片实际上是 把图片存在服务器的硬盘中,将图片存储的路径存在数据库中. 1 首先要配置文件上传的路径: 1.1 建立静态文件目录 在项目根目录下 新建一个 static文件夹,下面再建立一个med ...

  4. centos7搭建nexus maven私服(二)

    本文主要补充两个主题: 1.手动更新索引 2.通过maven客户端发布本地jar包到nexus 先说第一个主题: 由于maven中央仓库汇集了全世界绝大多数的组件,所以它的索引库非常庞大,在我们右击仓 ...

  5. *args和**kwargs

    #coding=utf8 __author__ = 'Administrator' # 当函数的参数不确定时,可以使用*args和**kwargs.*args没有key值,**kwargs有key值 ...

  6. Codeforces Round #436 (Div. 2) D. Make a Permutation!

    http://codeforces.com/contest/864/problem/D 题意: 给出n和n个数(ai <= n),要求改变其中某些数,使得这n个数为1到n的一个排列,首先保证修改 ...

  7. Java-Maven(二):Maven常用命令

    Maven命令简介 Maven提供了一套命令可以用来创建java工程.编译.打包等操作.通过这些命令来处理工作变得更方便.简洁. Maven工程结构和内容被定义在pom.xml文件中,全称projec ...

  8. QT生成随机数

    QT生成随机数和C语言差距不大,C语言用srand()和rand(),QT是用Qsrand()和qrand(): QT生成随机数的格式是: qsrand(QTime(0,0,0).secsTo(QTi ...

  9. UVA-10714 Ants---蚂蚁模拟

    题目链接: https://vjudge.net/problem/UVA-10714 题目大意: 给你一个长为L厘米的木棍在上面有n只蚂蚁,蚂蚁的爬行时间均为1厘米/秒,两只蚂蚁先遇会立即调转方向,调 ...

  10. WPF设置控件获得焦点

    1.这个比较有效 this.Dispatcher.BeginInvoke(DispatcherPriority.Background, (Action)(() => { Keyboard.Foc ...