Codeforces 544E K Balanced Teams (DP)
题目:
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.
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.
Print one integer — the maximum possible total number of students in no more than kk (and at least one) non-empty balanced teams.
5 2
1 2 15 15 15
5
6 1
36 4 1 25 9 16
2
4 4
1 10 100 1000
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)的更多相关文章
- Codeforces 1133E - K Balanced Teams - [DP]
题目链接:https://codeforces.com/contest/1133/problem/C 题意: 给出 $n$ 个数,选取其中若干个数分别组成 $k$ 组,要求每组内最大值与最小值的差值不 ...
- Codeforces Round #544 (Div. 3) E. K Balanced Teams (DP)
题意:有\(n\)个人,每个人的能力值是\(a_i\),现在你想将这些人分成\(k\)组(没必要全选),但是每组中最高水平和最低水平的人的能力差值必须\(\le 5\),问最多能选多少人. 题解:想了 ...
- codeforces 1133E K Balanced Teams
题目链接:http://codeforces.com/contest/1133/problem/E 题目大意: 在n个人中找到k个队伍.每个队伍必须满足最大值减最小值不超过5.求满足条件k个队伍人数的 ...
- CF1133E K Balanced Teams(DP)
/* 排序之后每个点往前能选择的是一段区间, 所以我们实际上转移位置是确定的 然后f[i][j]表示到了i选了j段的最大贡献, 显然状态数是O(n^2)的, 转移是O(1)的 */ #include& ...
- 【CF1133E】K Balanced Teams(动态规划,单调队列)
[CF1133E]K Balanced Teams(动态规划,单调队列) 题面 CF 让你把一堆数选一些出来分成不超过\(K\)组,每一组里面的最大值和最小值之差不超过\(5\),求最多有多少个人元素 ...
- K Balanced Teams CodeForces - 1133E (Dp)
题意: 给出 n 个数,选取其中若干个数分别组成至多 k 组,要求每组内最大值与最小值的差值不超过5,求最后被选上的总人数. 题解: 将a[1∼n] 从小到大排序, f[i][j] 表示到第 i 个数 ...
- E. K Balanced Teams
类比背包问题,为每个学生附加一个权重$pos[i]$,意思是选择该学生后,之后可以选择$p[i]~p[i]+5$的学生. 转换公式: $$d[i][j]=max(d[i+1][q],d[i+pos][ ...
- 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 ...
- Codeforces 1108D - Diverse Garland - [简单DP]
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per te ...
随机推荐
- 洛谷 P1091 合唱队形
\[传送门在这里呀\] 题目描述 \(N\)位同学站成一排,音乐老师要请其中的\((N-K)\)位同学出列,使得剩下的\(K\)位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次 ...
- CCPC-Wannafly Winter Camp Day1部分题目解析
Preface 最近恰好不知道做什么题,所以就按老叶要求做上面的比赛. 稍微看了下感觉难度适中,大部分题目偏向联赛难度,当然也有些题目打到了省选题的感觉(基本都是Div1的题) 这里就简单拿一些我做得 ...
- VBA Editor Addins --> VBE插件模板开发众筹
https://www.cnblogs.com/Charltsing/p/VBEAddins.html QQ:564955427 8月8日测试版功能说明 1.VBE菜单的创建 2.toolwindow ...
- webpack2.0 css文件引入错误解决及图片输出在根目录配置问题
webpack引入css文件,main.js内容如下 import Vue from 'vue'; import App from './App.vue'; import Mint from 'min ...
- Django初印象之视图(view)
一.view的初印象 一个视图函数(类),简称视图.我们发起web请求时,返回的web响应.[大家约定成俗将视图放置在项目(project)或应用程序(app)目录中的名为views.py的文件中.] ...
- 修改host,上github
操作如下: 1.http://ping.chinaz.com/ 搜索github.com 海外ip,其实能找到的就两个;然后再搜gist.github.com 海外ip,也是两个. 192.30.25 ...
- LIS的O(nlogn)算法
出自蓝书<算法竞赛入门经典训练指南> 求最长上升子序列是很常见的可以用动态规划解决的问题…… 很容易根据最优子结构之类的东西得出 $\text{dp}[i]$为以第i个数结尾的最长上升子序 ...
- mybatis操作mysql的奇淫技巧总结(代码库)
1.添加公共sql代码段 使用<sql> 和 <include> 标签 <sql id="userSubassemblyRecordParam"> ...
- Java【第五篇】基本语法之--数组
数组概述 数组是多个相同类型数据的组合,实现对这些数据的统一管理数组属引用类型,数组型数据是对象(Object),数组中的每个元素相当于该对象的成员变量数组中的元素可以是任何数据类型,包括基本类型和引 ...
- JDK动态代理(Proxy)的两种实现方式
JDK自带的Proxy动态代理两种实现方式 前提条件:JDK Proxy必须实现对象接口 so,创建一个接口文件,一个实现接口对象,一个动态代理文件 接口文件:TargetInterface.java ...