Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 633 Accepted Submission(s): 303
Problem Description
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)
Source
2017ACM/ICPC广西邀请赛-重现赛(感谢广西大学)
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX = 1e6+10;
int a[MAX];
int s[MAX];
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
memset(a,0,sizeof(a));
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
s[a[i]]++;
}
int ans=0;
for(int i=1;i<=MAX;i++)
{
if(s[i-1]==1&&s[i-2]==1&&s[i]>0)
{
s[i]--;
s[i-1]--;
s[i-2]--;
ans++;
}//遇到1 1 temp(temp>0)这种情况先按照顺子来计算。
ans+=s[i]/2;
s[i]=s[i]%2;//然后按对子计算
}
printf("%d\n",ans);
}
return 0;
}