Problem Description
Nike likes playing cards and makes a problem of it.
Now give you n integers, [Math Processing Error]
We define two identical numbers (eg: [Math Processing Error]) a Duizi,
and three consecutive positive integers (eg: [Math Processing Error]) 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 [Math Processing Error].
Each number can be used only once.
Now give you n integers, [Math Processing Error]
We define two identical numbers (eg: [Math Processing Error]) a Duizi,
and three consecutive positive integers (eg: [Math Processing Error]) 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 [Math Processing Error].
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( [Math Processing Error]).
Then the next line contains n space-separated integers [Math Processing Error] ( [Math Processing Error])
For each test case, the first line contains one integer n( [Math Processing Error]).
Then the next line contains n space-separated integers [Math Processing Error] ( [Math Processing Error])
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 2HintCase 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)思路:有顺子和对子,问我们应题中所给的数最多能组成多少副牌思路:在这里边我们要有的基本思想就是对子要比顺子性价比高,所以我们的策略就是,对于一个数x来说,如果他有k个,先用这k个组成k/2个对子,那么这个还剩下1个或者用完了,如果还剩下一个,并且x+1的个数是奇数,并且x+2存在,这样的话我们在组成一个对子,遍历一遍数组就ok了ac代码:#include <iostream> #include <cstdio> #include <cstring> using namespace std; int a[1000005]; int main() { int n; while(cin>>n) { int x; memset(a,0,sizeof(a)); int max1=-1; for(int i=1;i<=n;i++) { scanf("%d",&x); a[x]++; max1=max(max1,x); } int sum=0; for(int i=1;i<=max1;i++) { if(a[i]>1) { sum+=a[i]/2; a[i]=a[i]%2; } if(i<=max1-2&&a[i]==1&&a[i+1]%2!=0&&a[i+2]>=1) { sum++; a[i]--; a[i+1]--; a[i+2]--; } } cout<<sum<<endl; } return 0; }