Codeforces1256E_Yet Another Division Into Teams
题意
n个人,每人有一个能力值a[i],要求分成多个队伍,每个队伍至少3个人,使得所有队伍的max(a[i])-min(a[i])之和最小。
分析
- 不会巧妙的dp,想了一天只想到了暴力的dp。
- 先排序,设\(dp[i]\)表示到前i个数组队,所有队伍的最小极差和。
- 转移方程为\(dp[i]=min(dp[j-1]+a[i]-a[j])\)for j in 1...i-2。即\(dp[i]=a[i]+min(dp[j-1]-a[j])\)。
- 所以可以枚举i,然后用优先队列维护\(dp[j-1]-a[j]\),注意j最大是i-2。
- 为了方便最后输出方案,再维护一个len数组,表示前i个人当前i所在队伍的人数,最后从后往前递推,每次\(i-=len[i]-1\),就能标记队伍的分割点,然后类似差分的思想扫一遍即可得到答案。
- 还有10天就退役了,退役前不会dp,希望退役后能学会dp吧。
- 突然想到好像可以不用优先队列了,反正只要往一个方向扫同时维护一个最小值就可以了。
代码
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
typedef long long ll;
int n;
pair<ll,int> a[N];
ll dp[N];
int ans[N],vis[N],len[N];
queue<int> tmp;
struct node{
ll dp;
int i;
bool operator<(const node& rhs)const{
return dp>rhs.dp;
}
};
priority_queue<node> q;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lld",&a[i].first);
a[i].second=i;
}
sort(a+1,a+1+n);
dp[3]=a[3].first-a[1].first;
len[3]=3;
if(n>=4){
dp[4]=a[4].first-a[1].first;
len[4]=4;
tmp.push(4);
}
if(n>=5){
dp[5]=a[5].first-a[1].first;
len[5]=5;
tmp.push(5);
}
for(int i=6;i<=n;i++){
int t=tmp.front();
q.push(node{dp[t-1]-a[t].first,t});
tmp.pop();
auto mn=q.top();
dp[i]=a[i].first+mn.dp;
len[i]=i-mn.i+1;
tmp.push(i);
}
int team=0;
for(int i=n;i>=1;i--){
vis[i]=++team;
i-=len[i]-1;
}
int c=0;
for(int i=n;i>=1;i--){
if(vis[i]){
c=vis[i];
}
ans[a[i].second]=c;
}
printf("%lld %d\n",dp[n],team);
for(int i=1;i<=n;i++){
printf("%d%c",ans[i],i==n?'\n':' ');
}
return 0;
}
Codeforces1256E_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 ...
- Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划
Codeforces Round #598 (Div. 3)- E. Yet Another Division Into Teams - 动态规划 [Problem Description] 给你\( ...
- Yet Another Division Into Teams
E. Yet Another Division Into Teams 首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组. 这个题可以用动态规划来写,用一个数组来保存 ...
- Top 10 Universities for Artificial Intelligence
1. Massachusetts Institute of Technology, Cambridge, MA Massachusetts Institute of Technology is a p ...
- Codeforces Round #181 (Div. 2) B. Coach 带权并查集
B. Coach 题目连接: http://www.codeforces.com/contest/300/problem/A Description A programming coach has n ...
- Coach(并查集)
Description A programming coach has n students to teach. We know that n is divisible by 3. Let's ass ...
- WPF SDK研究 之 数据绑定
这一章介绍数据绑定.本章共计27个示例,全都在VS2008下.NET3.5测试通过,点击这里下载:ConnectedData.rar 1.ShowDataWithoutBinding注: <?M ...
- Codeforces Round #598 (Div. 3)
传送门 A. Payment Without Change 签到. Code /* * Author: heyuhhh * Created Time: 2019/11/4 21:19:19 */ #i ...
- CF598: div3解题报告
CF598:div3解题报告 A: Payment Without Change 思路: 按题意模拟即可. 代码: #include<bits/stdc++.h> using namesp ...
随机推荐
- ZOJ3649 Social Net
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3649 这题倍增维护信息之多,也能算是一道毒瘤题了-- 解题思路 ...
- A. Be Positive
A. Be Positive time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- java 手机号/身份证(*)加密隐藏中间某几位几位
//手机号 保留前3 后4 String phone = "18771632488"; System.out.println(phone.replaceAll("(\\d ...
- js中filter过滤用法总结
定义和用法 filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素. 注意: filter() 不会对空数组进行检测. 注意: filter() 不会改变原始数组 ...
- js中数组的经典特性
数组的创建 var arrayObj = new Array(); //创建一个数组 var arrayObj = new Array([size]); //创建一个数组并指定长度,注意不是上限,是长 ...
- 【Spark机器学习速成宝典】模型篇07梯度提升树【Gradient-Boosted Trees】(Python版)
目录 梯度提升树原理 梯度提升树代码(Spark Python) 梯度提升树原理 待续... 返回目录 梯度提升树代码(Spark Python) 代码里数据:https://pan.baidu.co ...
- sed 删除最后几行 和删除指定行 awk使用
sed 删除最后几行 和删除指定行 转载原文链接:http://blog.51cto.com/lspgyy/1305489 sed 想删除文件中的指定行,是可以用行号指定也可以用RE来匹配的. 删 ...
- golang defer 延后执行什么
对于golang的defer,我们已经知道,defer定义的语句可以延后到函数返回时执行. 经常用在文件的关闭,锁的释放等场景中.而且defer定义的语句即使遇到panic也会执行.这样,可以执行必要 ...
- spring BeanUtils.copyProperties只拷贝不为null的属性
在MVC的开发模式中经常需要将model与pojo的数据绑定,apache和spring的工具包中都有BeanUtils,使用其中的copyProperties方法可以非常方便的进行这些工作,但在实际 ...
- JS - neo4j-browser 初始化时运行命令的逻辑分析
背景 最近需要改点 neo4j-browser 的代码做个 demo,分析初始化时运行命令的代码时花了很多时间,记录一下. 目的 找出 dispatch SINGLE_COMMAND_QUEUED a ...