HDU - 6188 Duizi and Shunzi

版权声明:转载时标明一下出处啦QwQ https://blog.csdn.net/Zero_979/article/details/81539662

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6188

题目:

Nike likes playing cards and makes a problem of it. 

Now give you n integers, ai(1≤i≤n)ai(1≤i≤n) 

We define two identical numbers (eg: 2,22,2) a Duizi, 
and three consecutive positive integers (eg: 2,3,42,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)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≤1061≤n≤106). 
Then the next line contains n space-separated integers aiai (1≤ai≤n1≤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)

分析:

给你一组数,你要找对子和顺子一共多少。水题。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        int a;
        int m[100001];
        memset(m,0,sizeof(m));
        for(int i=0;i<n;i++){
            cin>>a;
            m[a]++;
        }
        int sum=0;
        for(int i=1;i<=n;){
            if(m[i]%2==1&&m[i+1]%2==1&&m[i+2]){sum++;m[i]--;m[i+1]--;m[i+2]--;}
            sum=sum+m[i]/2;
            i++;
        }
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Zero_979/article/details/81539662
hdu