【HDU-6188】Duizi and Shunzi

99 阅读1分钟
题意

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

 

样例
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

 



 

AC代码
#include<iostream>
#include<cstdio>
#include <string.h>
using namespace std;
int main()
{
    int n,a,count[100002];
     while(~scanf("%d",&n))    //while(1)会陷入死循环,一定不能用,用了就铁定超时。巨坑!!!
    {
        int ans=0;
        memset(count,0,sizeof(count));   //对数组初始化
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            count[a]++;   //记录每类牌的个数
        }
        for(int i=1;i<=n;i++)
        {
            ans+=count[i]/2;       //组成了count[i]/2个对子
            count[i]%=2;          //减去组成对子需要耗费的牌
            if(i<=n-2 && count[i]%2 && count[i+1]%2 && count[i+2])  //检查是否有组成顺子的条件
            {
                ans++;           //组成了一个顺子
                count[i]--;      //减去组成顺子需要耗费的牌
                count[i+1]--;
                count[i+2]--;
            }
        }
        printf("%d\n",ans);
    }
}


 

题源:acm.hdu.edu.cn/showproblem…