Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划

【Problem Description】
给你\(n\)个数,将其划分为多组,对于每个组定义其\(d\)值为 组内的最大值减最小值,问如何划分使得最终所有组的\(d\)值之和最小。每个组至少要保证有\(3\)个数。
【Solution】
将所有值从小到大排序,然后我们知道最多有\(5\)个人划分到同一组中,如果有\(6\)个人,那么划分为两组一定比划分为一组更优。
定义\(dp[i]\)表示前\(i-1\)个人划分后的最小\(d\)值和为\(dp[i]\),假设前\(i-1\)个人已经划分好了,然后就是确定哪些人与第\(i\)个人分为一组,题目要求至少\(3\)个人,而我们又知道最多\(5\)个人,所以枚举第\(j\in[i+2,i+4]\)个人,选择\(a[j]-a[i]\)最小的那个\(j\),将\([i,j]\)这些人分为一组即可。
【Code】
/*
* @Author: _Simon_
* @Date: 2019-11-06 10:55:21
* @Last Modified by: Simon
* @Last Modified time: 2019-11-06 10:55:21
*/
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxn 200005
#define INF 0x3f3f3f3f
pair<int,int>a[maxn];
int dp[maxn],p[maxn]; //dp[i]表示前i-1个人划分好后的最小d值和
int ans[maxn]/*每个人分在第几组*/,root,cnt/*总共有多少个组*/;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++) cin>>a[i].first,a[i].second=i;
sort(a+1,a+n+1); //从小到大排序
memset(dp,INF,sizeof(dp));dp[1]=0;
for(int i=1;i<=n;i++){
for(int j=2;j<=4&&i+j<=n;j++){
int diff=a[i+j].first-a[i].first;
if(dp[i+j+1]>dp[i]+diff){
dp[i+j+1]=dp[i]+diff;
p[i+j+1]=i; //记录方案,表示[i,i+j]这些人分为一组
}
}
}
root=n+1;cnt=1;
while(root!=1){
for(int i=root-1;i>=p[root];i--){ //[p[root], root-1]这些人为同一组
ans[a[i].second]=cnt;
}
cnt++;root=p[root]; //枚举下一组
}
cout<<dp[n+1]<<' '<<cnt-1<<endl;
for(int i=1;i<=n;i++) cout<<ans[i]<<' ';cout<<endl;
return 0;
}
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划的更多相关文章
- 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 ...
- 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】
https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...
- Codeforces Round #598 (Div. 3)E(dp路径转移)
题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- Codeforces Round #598 (Div. 3) F. Equalizing Two Strings 构造
F. Equalizing Two Strings You are given two strings s and t both of length n and both consisting of ...
- Codeforces Round #598 (Div. 3) D. Binary String Minimizing 贪心
D. Binary String Minimizing You are given a binary string of length n (i. e. a string consisting of ...
- Codeforces Round #598 (Div. 3) C. Platforms Jumping 贪心或dp
C. Platforms Jumping There is a river of width n. The left bank of the river is cell 0 and the right ...
- Codeforces Round #598 (Div. 3) B. Minimize the Permutation 贪心
B. Minimize the Permutation You are given a permutation of length n. Recall that the permutation is ...
- Codeforces Round #598 (Div. 3) A. Payment Without Change 水题
A. Payment Without Change You have a coins of value n and b coins of value 1. You always pay in exac ...
随机推荐
- Maya+VS编程入门初体验(HelloWorld)
Maya2018 + VS2017 环境搭建见 博客 1. 项目: VS 新建了一个 MEL Command类型的项目(MayaProject) 2. HelloWorld代码 #include< ...
- Bitmap通过getWidth和getHeight获取尺寸不符 -- 误以为是Matrix失效(哈)
参考:Bitmap通过getWidth和getHeight获取尺寸不符 因为项目需要我想做个这样的地图出来 这个图的的分辨率是1190 * 666的 在地图上定位,我底图是固定分辨率的,那么算个坐标就 ...
- java上传文件类型检测
在进行文件上传时,特别是向普通用户开放文件上传功能时,需要对上传文件的格式进行控制,以防止黑客将病毒脚本上传.单纯的将文件名的类型进行截取的方式非常容易遭到破解,上传者只需要将病毒改换文件名便可以完成 ...
- c#之break和continue的区别
break:跳出循环,执行循环外的语句: continue:跳出此次循环,进入下一次循环:
- [QT] - MjpegStreamer客户端(简易版)#工程源码
简介: 大学时期学习弄的一个小软件,可以起到示例的作用,软件的几个功能截图如正文所示,文末提供工程源码文件,感谢支持! 功能截图: [ 开发板启动 mjpg_streamer 服务器 ] [ 启动软件 ...
- 1.4 Linux下对lvm逻辑卷分区大小的调整(针对xfs和ext4不同文件系统)
当我们在安装系统的时候,由于没有合理分配分区空间,在后续维护过程中,发现有些分区空间不够使用,而有的分区空间却有很多剩余空间.如果这些分区在装系统的时候使用了lvm(前提是这些分区要是lvm逻辑卷 ...
- RSA非对称式加解密笔记
1.服务器生成[公钥]和[私钥],成对生成: 2.客户端生成证书信息,使用[公钥]进行加密,前提是有公钥,并生成证书信息: 3.客户端发送自身的计算机名.MAC.用户名.证书内容给服务器: 4.服务器 ...
- @PropertySource绝对路径(java.io.FileNotFoundException问题解决)
经常使用@PropertySource 来指定配置文件,然后@value获取配置参数: @Component @PropertySource(value= {"classpath:rules ...
- ZLC众利币系统APP开发
开发版本:APP 开发语言:php,java,.net 下面我们来看一下tp5 分页具体怎么用: 一, 分页简洁版 简洁分页仅仅只有上下页的分页输出,可以使用下面的简洁分页代码: // 查询状态为1的 ...
- python 开机 定时启动
Windows开机自动运行.py文件1.找到写好的.py文件,例如我的.py文件路径:D:\编程测试文件\untitled\03131105.py 2.选中文件03131105.py,右键——属性—— ...