Duizi and Shunzi
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 790 Accepted Submission(s): 359
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.
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 )
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
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
4
3
2
HintCase 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)
题意:给你一个序列问max(顺子数+对子数),两个相同的是一个对子,三个连着的是个顺子;
思路:看他人题解都是贪心着来,我又学习了一种是用栈来匹配,两个相同的是一个匹配,三个连着的是个匹配,然后就问最大的匹配数是多少;
代码:
#include<cstdio>
#include<cstring>
#include<stack>
#include<algorithm>
using namespace std;
int a[1000006],n;
int s[1000006];
int main()
{
while(scanf("%d",&n)!=EOF)
{
int top=1,ans=0;
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
{
s[top]=a[i];
if(top>=2)
{
if(s[top]==s[top-1])
{
ans++;
top-=2;
}
}
if(top>=3)
{
if(s[top]==s[top-1]+1&&s[top]==s[top-2]+2)
{
ans++;
top-=3;
}
}
top++;
}
printf("%d\n",ans);
}
return 0;
}