HDU - 6188 Duizi and Shunzi (贪心)

题目链接 https://vjudge.net/problem/HDU-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)

思路 1 2 3 3 4 5除这种情况外其它的情况优先对子

AC代码

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int s[1000005];
int main()
{   int n;
    while(cin>>n){
            int k;
            int sum=0;
        memset(s,0,sizeof(s));
        for(int i=0;i<n;i++){
            cin>>k;
            s[k]++;
        }
        for(int i=1;i<=1000005;i++){
            if(s[i]>0&&s[i-1]==1&&s[i-2]==1){
                sum++;
                s[i]--;
                s[i-1]--;
                s[i-2]--;
            }
            sum+=s[i]/2;
            s[i]%=2;
        }
        cout<<sum<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37291934/article/details/89335079