从贪心开始—HDU 6188

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)



 

 一点点从头开始。我们慢慢学贪心。嘻嘻贪恋韩容心。

对子:2张;

顺子:3张;

所以优先考虑对子。

前两张不可能存在顺子,所以挑对子;

从第三张开始,先判断能否与前面的剩下的牌形成顺子(只需要一张牌就使得cnt+1);

不能形成顺子再考虑对子。

要考虑最小花费,一定要每步最优化。


#include<cstdio>
#include<cstring>
using namespace std;
int a[1000010];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        for(int i = 0; i < n; i++)
        {
            int h;
            scanf("%d",&h);
            a[h]++;
        }
        int cnt = 0;
        for(int i = 1; i <= n; i++)
        {
            if(i <= 2) {
                cnt += a[i] / 2 ;
                a[i] %= 2;
            }
            else {
                if(a[i] && a[i-1] && a[i-2]){
                    a[i]--;a[i-1]--;a[i-2]--;
                    cnt++;
                }
                cnt+=a[i]/2;
                a[i] %= 2;
            }
        }
        printf("%d\n",cnt);
    }
}
这个题,我觉得网上的答案好有道理哈哈哈哈哈哈哈。啥时候我也自己做出来,让别人觉得有道理

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页