Nike likes playing cards and makes a problem of it.
Now give you n integers, ai(1≤i≤n)
We define two identical numbers (eg: 2,2) a Duizi,
and three consecutive positive integers (eg: 2,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).
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≤10^6).
Then the next line contains n space-separated integers aiai (1≤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<cstdio>
#include<cstring>
using namespace std;
const int maxn=1e6+5;
int a[maxn];
int main(){
int n;
while(~scanf("%d",&n)){
for(int i=1;i<=n;i++) //初始化
a[i]=0;
int x;
for(int i=1;i<=n;i++){
scanf("%d",&x);
a[x]++;
}
int ans=0;
for(int i=1;i<=n;i++){
ans+=a[i]/2;//对子数
a[i]%=2;
if(i+2<=n&&a[i]&&a[i+1]%2&&a[i+2]){//能否组成顺子且使结果最大
ans++;
a[i+1]--;
a[i+2]--;
}
}
printf("%d\n",ans);
}
return 0;
}