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≤106
).
Then the next line contains n space-separated integers ai (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)
题意:输入一个n,接下来有n个数,让你求出能组成最多的对子或者顺子的和。
题解:首先先满足对子,假设这个数有剩余,那么就看下一个个数是否为奇数,这样两个奇数组到第三个,即使第三个为偶数也不会亏,第一次写的时候判断的后两个都为奇数,然后一想 1 2 3333 4 5 这样3拿出两个 可以得到4个,第三个为奇数只会多,为偶数要么不变要么会多,所以只判断第二个就可以了
#include<iostream>
#include<cstdio>
using namespace std;
typedef long long ll;
const int N=1e6+10;
int n;
int a[N];
int main()
{
int x;
while(~scanf("%d",&n))
{
for(int i=0;i<=n+5;i++)a[i]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
a[x]++;
}
int ans=0;
for(int i=1;i<=n-2;i++)
{
ans+=a[i]/2;
a[i]%=2;
if(a[i])
{
if(a[i+1]&&a[i+2]&&(a[i+1]%2))
{
ans++;
a[i+1]--;
a[i+2]--;
}
}
}
ans+=a[n-1]/2;
ans+=a[n]/2;
printf("%d\n",ans);
}
return 0;
}