Codeforces Global Round 1 D. Jongmah

You are playing a game of Jongmah. You don't need to know the rules to solve this problem. You have nntiles in your hand. Each tile has an integer between 11 and mm written on it.

To win the game, you will need to form some number of triples. Each triple consists of three tiles, such that the numbers written on the tiles are either all the same or consecutive. For example, 7,7,77,7,7 is a valid triple, and so is 12,13,1412,13,14, but 2,2,32,2,3 or 2,4,62,4,6 are not. You can only use the tiles in your hand to form triples. Each tile can be used in at most one triple.

To determine how close you are to the win, you want to know the maximum number of triples you can form from the tiles in your hand.

Input

The first line contains two integers integer nn and mm (1≤n,m≤1061≤n,m≤106) — the number of tiles in your hand and the number of tiles types.

The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤m1≤ai≤m), where aiai denotes the number written on the ii-th tile.

Output

Print one integer: the maximum number of triples you can form.

Examples

input

Copy

扫描二维码关注公众号,回复: 5312169 查看本文章
10 6
2 3 3 3 4 4 4 5 5 6

output

Copy

3

input

Copy

12 6
1 5 3 3 3 4 3 5 3 2 3 3

output

Copy

3

input

Copy

13 5
1 1 5 1 2 3 3 2 4 2 3 4 5

output

Copy

4

Note

In the first example, we have tiles 2,3,3,3,4,4,4,5,5,62,3,3,3,4,4,4,5,5,6. We can form three triples in the following way: 2,3,42,3,4; 3,4,53,4,5; 4,5,64,5,6. Since there are only 1010 tiles, there is no way we could form 44 triples, so the answer is 33.

In the second example, we have tiles 11, 22, 33 (77 times), 44, 55 (22 times). We can form 33 triples as follows: 1,2,31,2,3; 3,3,33,3,3; 3,4,53,4,5. One can show that forming 44 triples is not possible.

题意:就是给你n个不超过m大小的数要你选出符合{x,x,x}或者{x-1,x,x+1}或者{x,x+1,x+2}或者{x-2,x-1,x}但{x,x+1,x+2}和{x-2,x-1,x}是等价的所以只要考虑其中一个就行还有x,x-1,x+1的个数大于等于3时{x-1,x,x+1}和{x,x,x},{x-1,x-1,x-1},{x+1,x+1,x+1}个数是等价的。所以我们dp的时候只要考虑个数小于等于2的情况(这个dp转移的关键).

思路:dp[i][j][k]表示以i组成的序列第一种有j个第二种有k个即({x-1,x,x+1}和{x,x+1,x+2});

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long

using namespace std;
const int maxn=1e6+100;
int cnt[maxn];
int dp[maxn][3][3];

int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        int x;
        scanf("%d",&x);
        cnt[x]++;
    }
    memset(dp,-1,sizeof(dp));
    dp[0][0][0]=0;
    for(int i=0;i<=m+1;i++)
    {
        for(int j=0;j<3;j++)
        {
            for(int k=0;k<3;k++)
            {
               if(dp[i][j][k]<0)
                    continue;
               int now=cnt[i+1]-j-k;
               for(int  t=0;t<3&&t<=now;t++)
               {
                   dp[i+1][k][t]=max(dp[i+1][k][t],dp[i][j][k]+((now-t)/3+t));
               }
            }
        }
    }
    printf("%d\n",dp[m+1][0][0]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/wxl7777/article/details/86929106