Duizi and Shunzi

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

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
 
 
题意:输入一个n,然后输入n张牌,每张牌都不大于n,问可以组成最多的对子数和顺子数,对子是两个相同的牌,顺子是连续的三张牌
思路:从1到n遍历, 如果第 i 张牌能打完对子还有多一张,那么看 i+1 张打完对子是不是可以多,如果多的话只要 i+2 有这张牌就可以打出一个顺子,在计算第 i 张牌的时候记得把对子的数量加进去,在可以组成顺子的时候记得把 第 i+1 和 i +2 凑顺子的那一张牌减掉。
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define PI acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=1e6+5;
int a[maxn];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(a,0,sizeof(a));
int t;
for(int i=0;i<n;i++)
{
scanf("%d",&t);
a[t]++;
}
int ans=0;
for(int i=1;i<=n;i++)
{
ans+=a[i]/2;
if(i<=n-2 && a[i]%2 && a[i+1]%2 && a[i+2])
{
ans++;
a[i]--;
a[i+1]--;
a[i+2]--;
}
}
printf("%d\n",ans);
}
}

Duizi and Shunzi HDU的更多相关文章

  1. Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC

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

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

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

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

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

  4. HDU 6188:Duizi and Shunzi(贪心)(广西邀请赛)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意 有n个数字,每个数字小于等于n,两个相同的数字价值为1,三个连续的数字价值为1 .问这n个 ...

  5. HDU 6188 Duizi and Shunzi 贪心

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188 题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续 ...

  6. HDU 6188 Duizi and Shunzi

    栈. 将数字排序后,一个一个压入栈.如果栈顶两个元素形成了对子,那么$ans+1$,弹出栈顶两个元素:如果栈顶三个元素形成了顺子,那么$ans+1$,弹出栈顶三个元素. #include<bit ...

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

    Problem Description Nike likes playing cards and makes a problem of it. Now give you n integers, ai( ...

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

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

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

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

随机推荐

  1. 洛谷P5534 【XR-3】等差数列 耻辱!!!

    题目描述 小 X 给了你一个等差数列的前两项以及项数,请你求出这个等差数列各项之和. 等差数列:对于一个 nnn 项数列 aaa,如果满足对于任意 i∈[1,n)i \in [1,n)i∈[1,n), ...

  2. Educational Codeforces Round 76 (Rated for Div. 2)F - Make Them Similar

    题意: 给你n个数字(<230),求出一个数字使得所有数字^这个数字之后,二进制状态下的1的个数相同. 解析: 因为最大数字二进制数有30位,所以分为前15位和后15位,分别枚举0-1<& ...

  3. 洛谷P3381 MCMF【网络流】

    题目描述 如题,给出一个网络图,以及其源点和汇点,每条边已知其最大流量和单位流量费用,求出其网络最大流和在最大流情况下的最小费用. 输入格式 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向 ...

  4. 小程序 与 App 与 H5 之间的区别

    小程序的实现原理 根据微信官方的说明,微信小程序的运行环境有 3 个平台,iOS 的 WebKit(苹果开源的浏览器内核),Android 的 X5 (QQ 浏览器内核),开发时用的 nw.js(C+ ...

  5. AspxDashboardView 更新参数

    AspxDashboardView 更新参数 function SetThrendDashboardView() { console.log("就是这样被你征服"); var to ...

  6. tcp与http协议 以及python的实现

    htpp协议 Rquest Headers格式: 请求方法空格URL空格协议版本回车符换行符 头部字段名:值回车符换行符 ··· 头部字段名:值回车符换行符 回车符换行符 请求数据 socket网络聊 ...

  7. docker镜像相关的常用操作

    1.保存镜像 #docker save 镜像名称 -o 保存的完整地址和文件名 docker save zhoushiya/zhiboyuan -o d:/zhiboyuan.tar 2.载入镜像 # ...

  8. c语言 memcpy()

    原文地址:https://blog.csdn.net/qq_21792169/article/details/50561570 头文件:#include <string.h> memcpy ...

  9. windows下pycharm输入法跟随设置

    参考网址:http://www.itdaan.com/blog/2018/05/20/90e64dae077f8ad7fa70bc9c3c8ab422.html

  10. C# 后台调用http,post访问url,获取数据

    1.封装post方法发送 using System; using System.Collections.Generic; using System.IO; using System.Linq; usi ...