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. 'object ''/usr/local/lib/libdns.so'' from /etc/ld.so.preload cannot be preloaded: ignored.'

    做了如下操作后: rm -rf xxx.jar kill -9 xx 重启Jar包,出现如下错误: ld.so: object '/usr/local/lib/libdns.so' from /etc ...

  2. siglongjmp和sigsetjmp 用法

    1. 引入原因 由于在信号处理期间自动屏蔽了正在被处理的信号,而使用setjmp/longjmp跳出信号处理程序时又不会自动将 信号屏蔽码修改会原来的屏蔽码,从而引起该信号被永久屏蔽. 可以使用sig ...

  3. Windows删除文件夹下的指定格式文件(递归删除)

    问题描述: 今天遇到一个需求,需要对文件夹进行文件筛选.目录结构较为复杂(目录较多,层次较深),数据量较大(总共60GB左右). 鉴于上述情况,直接排除了人工处理方式(否则小伙伴们会打死我的). 解决 ...

  4. OpenVAS开源风险评估系统部署方案

    OpenVAS,即开放式漏洞评估系统,是一个用于评估目标漏洞的杰出框架.功能十分强大,最重要的是,它是“开源”的——就是免费的意思啦- 它与著名的Nessus“本是同根生”,在Nessus商业化之后仍 ...

  5. Tomcat 的端口被占用的解决办法

    在dos下,输入  netstat   -ano|findstr  8080 //说明:查看占用8080端口的进程 显示占用端口的进程 taskkill  /pid  6856  /f //说明,运行 ...

  6. layui switch 确定之后才变更状态

    let x = data.elem.checked; data.elem.checked = !x; form.render(); 完整代码 form.on('switch(is_enable)', ...

  7. 【笔试题】Overloading in Java

    笔试题 Overloading in Java Question 1 以下程序的输出结果为( ). public class Test { public int getData() { return ...

  8. Listener学习

    监听器Listener用于监听web应用中某些对象.信息的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计 ...

  9. python 职责链模式

    模式定义 责任链模式(Chain of Responsibility Pattern)为请求创建了一个接收者对象的链. 这种模式给予请求的类型,对请求的发送者和接收者进行解耦. 这种类型的设计模式属于 ...

  10. C指针的一些小细节

    1  int *c;*c=4-->int *c;int b;c=&b;*c=4; 在使用指针之前,一定要将其初始化,当然,如果是赋予一个地址,就相当于使用的同时就进行了初始化.