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 - 动态规划的更多相关文章

  1. 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 ...

  2. 【CF1256】Codeforces Round #598 (Div. 3) 【思维+贪心+DP】

    https://codeforces.com/contest/1256 A:Payment Without Change[思维] 题意:给你a个价值n的物品和b个价值1的物品,问是否存在取物方案使得价 ...

  3. Codeforces Round #598 (Div. 3)E(dp路径转移)

    题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...

  4. Codeforces Round #598 (Div. 3)

    传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. Promise.resolve解析

    总结自:https://blog.csdn.net/lq15310444798/article/details/81275278 Promise.resolve返回一个Promise实例 参数分4种情 ...

  2. [PHP] Laravel 体现 MySQL、Sqlite 数据的大小写敏感

    要想实现数据比对的大小写敏感,方式是设置数据库字段类型 或者 字段校验字符集. MySQL 和 Sqlite 所支持的 collations 形式是不同的,你可以通过以下文档了解到. MySQL co ...

  3. mysq5.7 主主同步

    db01  172.21.0.10 db02  172.21.0.14 一.安装数据库看上一遍博客 修改配置文件  db01  172.21.0.10 [root@VM_0_10_centos mys ...

  4. Appium java-client库更新到6.x ,TouchAction类中弃用的函数及替代方法

    新版本的java-client已经取消swipe方法,很多TouchAction类中的很多老方法也都已经弃用,具体可以参考这边的官方说明文档: https://static.javadoc.io/io ...

  5. Spark学习(4) Spark Streaming

    什么是Spark Streaming Spark Streaming类似于Apache Storm,用于流式数据的处理 Spark Streaming有高吞吐量和容错能力强等特点.Spark Stre ...

  6. php怎么遍历关联和索引数组

    foreach $arr = ['a' => 1, 2, 3]; foreach($arr as $key => $value){ // } for $arr = [0, 1, 2, 3] ...

  7. S02_CH12_ AXI_Lite 总线详解

    S02_CH12_ AXI_Lite 总线详解 12.1前言 ZYNQ拥有ARM+FPGA这个神奇的架构,那么ARM和FPGA究竟是如何进行通信的呢?本章通过剖析AXI总线源码,来一探其中的秘密. 1 ...

  8. k8s集群node节点一直NotReady, 且node节点(并非master)的kubelet报错:Unable to update cni config: No networks found in /etc/cni/net.d

    若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11401031.html 问题: 集群搭建的过程中,master节点初始化成功,但 ...

  9. C# 读取Oracle数据库视图数据异常问题处理

    会出现类似现在这种提示的错误 System.Data.OracleClient 需要 Oracle 客户端软件 version 8.1.7 或更高版本 情况1.开发过程中遇到这种问题解决 由于.net ...

  10. SpringCloud微服务基础学习

    看了蚂蚁课堂的微服务学习,确实学习了不少关于微服务的知识,现在总结学习如下 : SpringCloud微服务基础单点系统架构传统项目架构传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一 ...