Duizi and Shunzi

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

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

 
 //2017-08-31
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
int arr[N], n;
bool book[N]; int main()
{
//freopen("input1007.txt", "r", stdin);
while(scanf("%d", &n) != EOF){
for(int i = ; i < n; i++)
scanf("%d", &arr[i]);
sort(arr, arr+n);
memset(book, , sizeof(book));
int ans = ;
for(int i = ; i < n; i++){
if(i >= ){
int p1 = -, p2 = -;
for(int j = i-; j >= ; j--){
if(arr[j] == arr[i]- && !book[j]){
p1 = j;
}
if(arr[j] == arr[i]- && !book[j]){
p2 = j;
break;
}
if(arr[j] < arr[i]-)break;
}
if(p1 != - && p2 != -){
ans++;
book[i] = book[p1] = book[p2] = ;
}
}
if(arr[i-] == arr[i] && !book[i-] && !book[i]){
ans++;
book[i-] = book[i] = ;
}
}
printf("%d\n", ans);
} return ;
}

HDU6188的更多相关文章

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

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

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

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

  3. hdu6188&&百度之星初赛(B) T5

    度度熊的交易计划 Problem Description 度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生 ...

  4. CF-1110 (2019/02/08)

    CF-1110 A. Parity 快速幂的思想,考虑最后一位即可 #include <bits/stdc++.h> using namespace std; typedef long l ...

随机推荐

  1. Linux Shell常用脚本整理

    轮询检测Apache状态并启用钉钉报警◆ #!/bin/bash shell_user="root" shell_domain="apache" shell_l ...

  2. [每天解决一问题系列 - 0002] Xcopy cannot copy file with long directory

    现象: 当xcopy的文件的全名(包括目录和文件名)的长度超过255字符时,会copy失败,得到insufficient memory错误 解决方法: 在Server 版的OS中,有robcopy命令 ...

  3. Why does Http header contains "X-SourceFiles"?

    Question: Using a FileStreamResult in ASP.NET MVC 3, I get a response header like X-SourceFiles =?UT ...

  4. C#:使用ListView动态添加数据一直闪烁的解决办法

    首先,自定义一个类ListViewNF,继承自 System.Windows.Forms.ListView using System; using System.Collections.Generic ...

  5. maven更换源

    1)在 /etc/maven/settings.xml 找到  <mirrors>  </ mirrors>标签,在标签内部 添加内容如下: <mirror>    ...

  6. pcm原始数据绘制

    最近帮别人做了个东西,这里分享一下pcm原始数据绘图的思路 1.pcm数据采样位数,根据采样位数选取适合自己绘图的采样点的数量 2.计算出最大最小的的采样点的值差 3.根据要显示pcm数据的控件宽高, ...

  7. 关于C++11中的std::move和std::forward

    std::move是一个用于提示优化的函数,过去的c++98中,由于无法将作为右值的临时变量从左值当中区别出来,所以程序运行时有大量临时变量白白的创建后又立刻销毁,其中又尤其是返回字符串std::st ...

  8. oc中类的实例化及方法调用

    上一篇我们讲了oop和类的创建,上一篇的重点我们回顾一下 类 对象 实例 方法 接口 这一篇我们来实现类的实例化,调用类中的公共参数和方法:类的实现在.m文件中,以下是实现代码: // // HuiT ...

  9. 使用java调用fastDFS客户端进行静态资源文件上传

    一.背景 上篇博客我介绍了FastDFS的概念.原理以及安装步骤,这篇文章我们来聊一聊如何在java中使用FastDFSClient进行静态资源的上传. 二.使用步骤 1.开发环境 spring+sp ...

  10. 自然语言处理--LDA主题聚类模型

    LDA模型算法简介: 算法 的输入是一个文档的集合D={d1, d2, d3, ... , dn},同时还需要聚类的类别数量m:然后会算法会将每一篇文档 di 在 所有Topic上的一个概率值p:这样 ...