标签:链接 printf int define 个数 getch ret pre getchar
神仙\(dp\),不知道怎么想到的状态……
统计一下每个数出现的个数,设为\(cnt_i\)
发现如果有三个三元组\([a - 1, a, a + 1]\),那么可以拆分成三个\(a - 1\),三个\(a\)和三个\(a + 1\),所以只考虑前面的三元组即可
那么设\(dp_{i, j, k}\)表示\(dp\)到第\(i\)个数,其中关于\(i\)的三元组有\(j\)个,关于\(i + 1\)的三元组有\(k\)个的最多三元组个数
\(dp\)方程打起来有点窒息,看代码吧
#include<bits/stdc++.h>
#define N (int)1e6 + 5
using namespace std;
inline int read() {
int cnt = 0, f = 1; char c = getchar();
while (!isdigit(c)) {if (c == '-') f = -f; c = getchar();}
while (isdigit(c)) {cnt = (cnt << 3) + (cnt << 1) + (c ^ 48); c = getchar();}
return cnt * f;
}
int dp[2][3][3], n, m, c[N];
int main() {
n = read(), m = read();
for (register int i = 1; i <= n; ++i) ++c[read()];
for (register int i = 1; i <= m + 2; ++i) {
int p = i & 1, q = p ^ 1;
memset(dp[p], 0, sizeof dp[p]);
for (register int j = 0; j < 3; ++j)
for (register int k = 0; k < 3; ++k)
for (register int l = 0; l < 3; ++l)
if (c[i] - l - j - k >= 0) dp[p][j][k] = max(dp[p][j][k], dp[q][l][j] + (c[i] - l - j - k) / 3 + k);
}
printf("%d", dp[m & 1][0][0]);
return 0;
}
标签:链接 printf int define 个数 getch ret pre getchar
原文地址:https://www.cnblogs.com/kma093/p/11792140.html