Duizi and Shunzi

Nike likes playing cards and makes a problem of it. 

Now give you n integers,  ai(1in)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. 
InputThe input contains several test cases. 

For each test case, the first line contains one integer n( 1n1061≤n≤106). 
Then the next line contains n space-separated integers  aiai ( 1ain1≤ai≤n
OutputFor 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

这个题刚开始想对子有两个,顺子有三个,那么肯定对子节省牌,其实感觉这种题贼像cf的题,但是这种在样例3就过不了了,最后想了想,能凑出个顺子,那先把前面的对子取出来,最后剩下一个顺子就是一个合理的贪心策略。


#include<bits/stdc++.h>

using namespace std;

int a[100005];
int book[100005];

int main(){
    int n;
    while(~scanf("%d",&n)){
        memset(book,0,sizeof(book));
        int maxn=-1;
        for(int i=0;i<n;i++){
            int k;
            scanf("%d",&k);
            book[k]++;
            maxn=max(maxn,k);
        }
        int res=0;
        for(int i=3;i<=maxn+3;i++){
            if(book[i-1]>=2){
                if(book[i-1]%2==0){
                    res+=book[i-1]/2;
                    book[i-1]=0;
                }else if(book[i-1]%2==1){
                    res+=book[i-1]/2;
                    book[i-1]=1;
                }
            }
            if(book[i-2]>=2){
                if(book[i-2]%2==0){
                    res+=book[i-2]/2;
                    book[i-2]=0;
                }else if(book[i-2]%2==1){
                    res+=book[i-2]/2;
                    book[i-2]=1;
                }
            }
            if(book[i-2]&&book[i-1]&&book[i]){
                book[i-2]--;
                book[i-1]--;
                book[i]--;
                res++;
            }
        }
        printf("%d\n",res);
    }
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhouzi2018/article/details/81042765