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 ...
随机推荐
- Python3 获取一大段文本之间两个关键字之间的内容
用re或者string.find.以下是re代码 123456789101112131415import re#文本所在TXT文件file = '123.txt' #关键字1,2(修改引号间的内容)w ...
- Spring Boot教程(三十)使用Spring-data-jpa(1)
在实际开发过程中,对数据库的操作无非就“增删改查”.就最为普遍的单表操作而言,除了表和字段不同外,语句都是类似的,开发人员需要写大量类似而枯燥的语句来完成业务逻辑. 为了解决这些大量枯燥的数据操作语句 ...
- Mysql cluster管理节点配置文件详解
一.定义MySQL Cluster的TCP/IP连接TCP/IP是MySQL集群用于建立连接的默认传输协议,正常情况下不需要定义连接.可使用“[TCP DEFAULT]”或“[TCP]”进行定义. 1 ...
- input(移动端iOS)输入内容时调用软件盘后页面底部留白问题
iOS/input输入框调用软键盘底部留白 只需input输入框失去焦点时,让页面自动下移即可恢复 <input placeholder="请输入用户名" v-model=& ...
- 自动化部署脚本之windows上执行批处理文件
windows .bat 批处理 脚本路径如下: install-simo.bat文件内容: @ECHO OFF set scriptpath=%~dp0set logfile=%scriptpa ...
- golang string、int、int64 float 互相转换
#string到int int,err := strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, ...
- Dijk入门(杭电2544题)
#include<iostream> #include<cstring> using namespace std; #define INF 0x3f3f3f3f int n,m ...
- hdjs---1、hdjs爬坑杂记
hdjs---1.hdjs爬坑杂记 一.总结 一句话总结: 对hdjs这种文档和完善都不是很好的插件,应该先在项目的空页面试,成功后再用到用了框架的项目中 1.hdjs4.0.18引入select2? ...
- HearthBuddy 第一次调试
HearthBuddy https://www.jiligame.com/70639.html 解压缩包,打开hearthbuddy.exe直接运行就可以:不用替换mono.dll直接可用:不需要校验 ...
- [NLP] 语义网络与知识图谱入门(二)
语义网络与知识图谱入门(二) OWL 本体声明 owl用owl:Ontology来声明一个本体.rdf:about属性为本体提供一个名称或引用.根据标准,当rdf:about属性的值为"&q ...