hdu 6188 Duizi and Shunzi(贪心)

Duizi and Shunzi

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1507    Accepted Submission(s): 627


Problem Description
Nike likes playing cards and makes a problem of it.

Now give you n integers, ai(1in)

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( 1n106 ).
Then the next line contains n space-separated integers ai ( 1ain )
 

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

题意:

给了一些牌,两张牌可以组成一个对子,连续的三张牌能组成一个顺子。每张牌只能使用一次,问最多组成对子和顺子的和为多少。

思路:

可以想到对子优先考虑,然后再考虑成顺子。所以对于第一张牌,有对子成对子,如果对子匹配完了后还剩一张牌就要考虑和后面的牌匹配成顺子。而如果第二张牌是奇数的,那么直接匹配更优因为已经能确定它前面有一张和它匹配了,只需要后面的牌是否匹配。而如果第二张牌是偶数的话,如果匹配,破坏了一次成对机会并且要求第二张以后还要有与第二张匹配的牌。如1,2,2,2,2,3,3这样的情况。所以第二张牌必须也为奇数。而第三张牌的奇偶性没有要求,因为如果第三张牌数为奇数,顺子匹配完了,只剩偶数了,这是我们想要的。如果第三张牌为偶数k,如果不匹配它自己成对会有k/2次,如果匹配顺子加成的对子是(k-1)/2+1仍然是k/2,并且还多提供了一张牌与后面的牌匹配顺子。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int maxn=1000005;
int a[maxn];
int mp[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(mp,0,sizeof(mp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            mp[a[i]]++;
        }
        sort(a+1,a+1+n);
        int cnt=0;
        for(int i=1;i<=n;i++)
        {
            if(mp[a[i]]>=2)
            {
                cnt+=mp[a[i]]/2;
                mp[a[i]]%=2;
            }
            if(mp[a[i]]==1&&mp[a[i]+1]%2==1&&mp[a[i]+2]>=1)
            {
                cnt++;
                mp[a[i]]--;
                mp[a[i]+1]--;
                mp[a[i]+2]--;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值