Codeforces Global Round 1 D. Jongmah(DP)

D. Jongmah
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are playing a game of Jongmah. You don’t need to know the rules to solve this problem. You have n tiles in your hand. Each tile has an integer between 1 and m 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,7 is a valid triple, and so is 12,13,14, but 2,2,3 or 2,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 n and m (1≤n,m≤106) — the number of tiles in your hand and the number of tiles types.

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

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

Examples
inputCopy
10 6
2 3 3 3 4 4 4 5 5 6
outputCopy
3
inputCopy
12 6
1 5 3 3 3 4 3 5 3 2 3 3
outputCopy
3
inputCopy
13 5
1 1 5 1 2 3 3 2 4 2 3 4 5
outputCopy
4
Note
In the first example, we have tiles 2,3,3,3,4,4,4,5,5,6. We can form three triples in the following way: 2,3,4; 3,4,5; 4,5,6. Since there are only 10 tiles, there is no way we could form 4 triples, so the answer is 3.

In the second example, we have tiles 1, 2, 3 (7 times), 4, 5 (2 times). We can form 3 triples as follows: 1,2,3; 3,3,3; 3,4,5. One can show that forming 4 triples is not possible.

题意大概是,给你n个最大值为m的数,每次可以取三个连续的数或者三个相同的数,求能取的最大数。我一开始总觉得这个题我在哪见过,当时是个贪心,后来着手写了之后发现不对,这个题只能dp。
这个题dp的要点在于,取三组个相同的连续数字相当于取三组连续的相同数字,所以(n,n+1,n+2)这种,最多可以出现两次,然后开始dp。
dp[i][j][k]代表在i这个数之前,取了j个(i-1,i,i+1)和k个(i,i+1,i+2)的情况下,最多的组数。
所以当前剩下的没有被取完的i就应该是cnt[i]-j-k。转移方程是dp[i + 1][k][t] = max(dp[i + 1][k][t], dp[i][j][k] + (now - t) / 3 + t);
非常巧妙,希望什么时候我也能自己写出来一次dp。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<list>
#include<math.h>
#include<vector>
#include<stack>
#include&
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值