欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Duizi and Shunzi HDU - 6188 (贪心)2017 广西ACM/ICPC

程序员文章站 2022-04-18 11:54:19
Duizi and Shunzi Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2883 Accepted Submission(s): 111 ......

duizi and shunzi

time limit: 6000/3000 ms (java/others)    memory limit: 32768/32768 k (java/others)
total submission(s): 2883    accepted submission(s): 1110


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
 
 
题意:输入一个n,然后输入n张牌,每张牌都不大于n,问可以组成最多的对子数和顺子数,对子是两个相同的牌,顺子是连续的三张牌
思路:从1到n遍历, 如果第 i 张牌能打完对子还有多一张,那么看 i+1 张打完对子是不是可以多,如果多的话只要 i+2 有这张牌就可以打出一个顺子,在计算第 i 张牌的时候记得把对子的数量加进去,在可以组成顺子的时候记得把 第 i+1 和 i +2 凑顺子的那一张牌减掉。
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define eps 1e-10
#define pi acos(-1.0)
#define _e exp(1.0)
#define ll long long
const int maxn=1e6+5;
int a[maxn];
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        memset(a,0,sizeof(a));
        int t;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&t);
            a[t]++;
        }
        int ans=0;
        for(int i=1;i<=n;i++)
        {
            ans+=a[i]/2;
            if(i<=n-2 && a[i]%2 && a[i+1]%2 && a[i+2])
            {
                ans++;
                a[i]--;
                a[i+1]--;
                a[i+2]--;
            }
        }
        printf("%d\n",ans);
    }
}