题目:

You are a coach at your local university. There are nn students under your supervision, the programming skill of the ii-th student is aiai.

You have to form kk teams for yet another new programming competition. As you know, the more students are involved in competition the more probable the victory of your university is! So you have to form no more than kk (and at least one) non-empty teams so that the totalnumber of students in them is maximized. But you also know that each team should be balanced. It means that the programming skill of each pair of students in each team should differ by no more than 55. Teams are independent from one another (it means that the difference between programming skills of two students from two different teams does not matter).

It is possible that some students not be included in any team at all.

Your task is to report the maximum possible total number of students in no more than kk (and at least one) non-empty balanced teams.

If you are Python programmer, consider using PyPy instead of Python when you submit your code.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤50001≤k≤n≤5000) — the number of students and the maximum number of teams, correspondingly.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is a programming skill of the ii-th student.

Output

Print one integer — the maximum possible total number of students in no more than kk (and at least one) non-empty balanced teams.

Examples
input
5 2
1 2 15 15 15
output
5
input
6 1
36 4 1 25 9 16
output
2
input
4 4
1 10 100 1000
output
4

题意:
有n个学生 要求组成k个小组 每个小组中两两差值不得超过5 可以有学生不被编入组中 求最多可以有多少个学生被编入组中 思路:
采取线性dp的思路
将每个学生的值a[i]从小到大排序
排序后 每个小组必定可以视为一段连续区域 则将pos设为一段连续区间的左端点
dp[i][j]表示到第i个学生为止 前面分为j组的最多学生数
状态转移方程为
dp[i][j]=max(dp[i-][j],dp[pos-][j-]+i-pos+);

代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm> using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=;
int n,k;
int a[maxn],dp[maxn][maxn]; int main(){
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
sort(a+,a++n);
int pos=;
dp[][]=;
for(int i=;i<=n;i++){
while(a[i]-a[pos]> && pos<=n) pos++;
for(int j=;j<=min(i,k);j++){
dp[i][j]=dp[i-][j];
dp[i][j]=max(dp[i][j],dp[pos-][j-]+i-pos+);
}
}
int ans=;
for(int i=;i<=k;i++){
ans=max(ans,dp[n][i]);
}
printf("%d\n",ans);
return ;
}

Codeforces 544E K Balanced Teams (DP)的更多相关文章

  1. Codeforces 1133E - K Balanced Teams - [DP]

    题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...

  2. Codeforces Round #544 (Div. 3) E. K Balanced Teams (DP)

    题意:有\(n\)个人,每个人的能力值是\(a_i\),现在你想将这些人分成\(k\)组(没必要全选),但是每组中最高水平和最低水平的人的能力差值必须\(\le 5\),问最多能选多少人. 题解:想了 ...

  3. codeforces 1133E K Balanced Teams

    题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...

  4. CF1133E K Balanced Teams(DP)

    /* 排序之后每个点往前能选择的是一段区间, 所以我们实际上转移位置是确定的 然后f[i][j]表示到了i选了j段的最大贡献, 显然状态数是O(n^2)的, 转移是O(1)的 */ #include& ...

  5. 【CF1133E】K Balanced Teams(动态规划,单调队列)

    [CF1133E]K Balanced Teams(动态规划,单调队列) 题面 CF 让你把一堆数选一些出来分成不超过\(K\)组,每一组里面的最大值和最小值之差不超过\(5\),求最多有多少个人元素 ...

  6. K Balanced Teams CodeForces - 1133E (Dp)

    题意: 给出 n 个数,选取其中若干个数分别组成至多 k 组,要求每组内最大值与最小值的差值不超过5,求最后被选上的总人数. 题解: 将a[1∼n] 从小到大排序, f[i][j] 表示到第 i 个数 ...

  7. E. K Balanced Teams

    类比背包问题,为每个学生附加一个权重$pos[i]$,意思是选择该学生后,之后可以选择$p[i]~p[i]+5$的学生. 转换公式: $$d[i][j]=max(d[i+1][q],d[i+pos][ ...

  8. Codeforces Round #598 (Div. 3) E. Yet Another Division Into Teams dp

    E. Yet Another Division Into Teams There are n students at your university. The programming skill of ...

  9. Codeforces 1108D - Diverse Garland - [简单DP]

    题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...

随机推荐

  1. 货车运输-洛谷-1967-LCA+最大生成树(kruskal(并查集))

    传送门 一道:LCA+最大生成树 个人认为把这两个的板子写好(并熟练掌握了之后)就没什么难的 (但我还是de了好久bug)qwq 最大生成树:其实就是最小生成树的变形 我用的是kruskal (个人觉 ...

  2. HBase原理分析

    宏观架构 HBase从宏观上看只有HMaster.RegionServer和zookeeper三个组件. Master: 负责启动的时候分配Region到具体的RegionServer,执行各种管理操 ...

  3. WinForm程序完全退出总结

    this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: Application.Exit();  强制所有消息中止,退出 ...

  4. UOJ129 NOI2015 寿司晚宴 数论、状压DP

    传送门 数论题\(n \leq 500\)肯定是什么暴力算法-- 注意到每一个数\(> \sqrt{n}\)的因子最多只有一个,这意味着\(> \sqrt{n}\)的因子之间是独立的,而只 ...

  5. webpack4配置详解之新手上路初探

    前言 经常会有群友问起webpack.react.redux.甚至create-react-app配置等等方面的问题,有些是我也不懂的,慢慢从大家的相互交流中,也学到了不少. ​ 今天就尝试着一起来聊 ...

  6. C#模板设计模式使用和学习心得

    模板设计模式: 模版方法模式由一个抽象类和一个(或一组)实现类通过继承结构组成,抽象类中的方法分为三种: 抽象方法:父类中只声明但不加以实现,而是定义好规范,然后由它的子类去实现. 模版方法:由抽象类 ...

  7. vue 项目设置实现通过本地手机访问

    1.查询ip地址 win+R打开cmd 窗口 输入ipconfig 找到IPV4地址   192.168.x.xxx 2.在vue项目config文件夹中的index.js,将localhost换为i ...

  8. 排序算法(sorting)

    学习到的排序算法的总结,包括对COMP20003中排序部分进行总结,部分图片来自COMP20003 有部分内容来自http://www.cnblogs.com/eniac12/p/5329396.ht ...

  9. Kafka-python 客户端导致的 cpu 使用过高,且无法消费消息的问题

    今天遇到一个情况使用了 Kafka-python 1.3.3 来操作读取 broker 1.0.1 版本的 kafka.出现了 rebalance 之后分配到了客户端,但是 cpu 利用率很高且无法消 ...

  10. SQLServer 中多行数据合并成一行数据(一个字段)

    需求:有四行数据,如下: 1.苹果 2.橘子 3.桃子 4.波罗 合并成一个字段:苹果,橘子,桃子,波罗: 需求明确之后,先弄点测试数据,上代码: --创建一个临时表 Create table #te ...