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< ...
- C#.NET XML 与 实体 MODEL 互转,非序列化
只能处理简单结构XML 和 实体. using System.Text; using System.Xml; namespace A.Util { public static class MyXmlU ...
- 【Spring Cloud学习之四】Zuul网关
环境 eclipse 4.7 jdk 1.8 Spring Boot 1.5.2 Spring Cloud 1.2 一.接口网关接口网关:拦截所有的请求,交由接口网关,然后接口网关进行转发,类似ngi ...
- netty中的channelPipeline在编程中的作用
在netty编程中我们绝大多数是要是用nio的,nio相比传统的io更加高效,而nio中核心概念离不开channel,buffer,selector三个重要的对象. 那么在netty中有一个chann ...
- 'cross-env' 不是内部或外部命令,也不是可运行的程序 或批处理文件。 npm ERR! code ELIFECYCLE npm ERR! errno 1
在建立vue项目时报的错误 原因是缺少依赖,在此项目目录下下载依赖即可 cnpm install 发一次建立vue项目的完整顺序吧 //打开要创建的项目路径目录,创建项目 vue init webpa ...
- AtCoder-abc147 (题解)
A - Blackjack (水题) 题目链接 大致思路: 水题 B - Palindrome-philia (水题) 题目链接 大致思路: 由于整个串是回文串,只要判断前一半和后一半有多少个不同即可 ...
- Go grpc 与 protobuf
现在很多微服务内部的通信协议都采用rpc,性能高,安全.而grpc则是google退出的rpc plus. protobuf是传输协议,性能高,强大. 来一个server client的通信demo, ...
- Flannel 原理简析及安装
flannel是CoreOS提供用于解决Dokcer集群跨主机通讯的覆盖网络工具.它的主要思路是:预先留出一个网段,每个主机使用其中一部分,然后每个容器被分配不同的ip:让所有的容器认为大家在同一个直 ...
- (转)数据库_不懂数据库索引的底层原理?那是因为你心里没点BTree
原文地址:https://www.cnblogs.com/sujing/p/11110292.html 要了解数据库索引的底层原理,我们就得先了解一种叫树的数据结构,而树中很经典的一种数据结构就是二叉 ...
- C# vb .net实现过度曝光效果滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的过度曝光效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...