HDU 6188 Duizi and Shunzi 贪心

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6188

题意:给了n个数,然后现在问我们最多构成多少个对子和顺子,其中对子是2个相同的牌,顺子是3个连续的牌。

解法:考虑贪心,我们尽量放对子,但是有一种特殊情况就是,我们可以退一个对子出来和周围的相邻点构成顺子。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+6;
int cnt[maxn];
int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        memset(cnt, 0, sizeof(cnt));
        for(int i=1; i<=n; i++){
            int x;
            scanf("%d", &x);
            cnt[x]++;
        }
        int ans=0;
        ans+=cnt[1]/2;
        ans+=cnt[2]/2;
        cnt[1]%=2;
        cnt[2]%=2;
        for(int i=3; i<=n; i++){
            if(cnt[i]&&cnt[i-1]&&cnt[i-2]){
                ans++;
                cnt[i]--;
                cnt[i-1]=cnt[i-2]=0;
            }
            ans+=cnt[i]/2;
            cnt[i]%=2;
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/spfa/p/7486608.html