hdu 6188

来源:互联网 发布:nginx负载均衡配置详解 编辑:程序博客网 时间:2024/04/29 20:21

题意:给你一些牌,要你找到最大的对子和顺子数目(牌不能重复使用)

分析 : 贪心,可以考虑先排序,思路更清楚

考虑当前数为奇数或偶数,当为奇数时,可能出现顺子情况,考虑接下来两张牌,如果第二张也为奇数并且第三张存在的话,组成一个顺子(第三张即使时偶数,也不会影响结果)

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <map>using namespace std;typedef long long int ll;#define maxn 1000005map<int,int>mp;int n;int a[maxn];int ans;int main(){    while( cin >> n )    {        ans = 0;        mp.clear();        for(int i = 0 ; i < n ; ++i)        {            scanf("%d",a+i);            mp[a[i]]++;        }        sort(a,a+n);        for(int i = 0 ; i < n ; ++i)        {            int x = a[i];            ans += mp[x]/2;            mp[x] = mp[x]%2;            if( mp[x] && mp[x+1]%2 && mp[x+2] )            {                ans ++;                mp[x]--;                mp[x+1]--;                mp[x+2]--;            }        }        cout << ans << endl;    }}