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.
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 )
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
2
4
3
2
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)
题解:
题意:
给你一堆牌,让你求出最大的对子数和顺子数(3个连续的牌)的和
思路:
从右到左扫一遍牌,先把对子打掉,如果还剩下牌,就看下一个数字的牌数是否为奇数,如果是并且下下张牌是有牌的,就连成顺子打出去,这样贪心思路其实是用“废弃"的当前和废弃的下一个奇数牌,使得消耗一个有用的下下张牌就能换成一对,而如果是只打下下张牌的对子那么就是消耗了2张有用的牌(暂时判定)来换来一个对子
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<math.h>
#include<string>
#include<stdio.h>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<deque>
#include<algorithm>
using namespace std;
#define INF 100861111
#define ll long long
#define eps 1e-7
#define maxn 1e5+5
#define lson k*2
#define rson k*2+1
#define M (t[k].l+t[k].r)/2
int a[1000005];
int main()
{
int i,j,n,ans,x;
while(scanf("%d",&n)!=EOF)
{
ans=0;
memset(a,0,sizeof(a));
for(i=1;i<=n;i++)
{
scanf("%d",&x);
a[x]++;
}
for(i=1;i<=n;i++)
{
ans+=(a[i]/2);
a[i]%=2;
if(a[i]&&a[i+1]%2&&a[i+2])
{
ans++;
a[i]--;
a[i+1]--;
a[i+2]--;
}
}
printf("%d\n",ans);
}
return 0;
}