AcWing 3583. 整数分组
原题链接
中等
作者:
silly_xl
,
2021-05-29 22:02:31
,
所有人可见
,
阅读 347
(动态规划 + 双指针)
C++ 代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 5010;
int n, m;
int w[N];
int f[N][N];
int main()
{
cin >> n >> m;
for (int i = 1; i <= n; i ++ ) scanf("%d", &w[i]);
sort(w + 1, w + n + 1);
for (int i = 1, k = 1; i <= n; i ++ )
{
while(w[i] - w[k] > 5) k ++;
for (int j = 1; j <= m; j ++ )
f[i][j] = max(f[i - 1][j], f[k - 1][j - 1] + (i - k + 1));
}
cout << f[n][m] << endl;
return 0;
}