Yet Another Division Into Teams
E. Yet Another Division Into Teams
首先要想明白一个东西,就是当一个小组达到六个人的时候,它一定可以拆分成两个更优的小组。
这个题可以用动态规划来写,用一个数组来保存状态,用一个队列来尝试新的状态,但是因为上面的这个特性,每一次只会有三个新的状态。
我们用
sum来保存躲避选择的元素,举个例子:分组情况为:1 2 3 | 5 6 8 11 | 20 21 22 (不一定满足题意,只是为了说明
sum的意义)则
sum=(5-3)+(20-11)=11那么这样分组的总代价为:
a[n]-a[1]-sum=22-1-11那么我们的目的就是使
sum尽可能的大,这样分组的代价才会尽可能的小。另外还需要注意一点,就是三人才能成一组,所以就需要三个变量来进行一个轮回,来保证至少三人一组。
for(int i=4;i<=n;++i)
{
pre[i]=sum.se;
pii temp={sum.fi+a[i].fi-a[i-1].fi, i};
sum=max(sum,t1);
t1=t2,t2=temp;
}
代码:
// Created by CAD on 2019/11/6.
#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<long long,int>
using namespace std;
const int maxn=2e5+5;
pii a[maxn];
int pre[maxn];
int out[maxn];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n; cin>>n;
for(int i=1;i<=n;++i)
cin>>a[i].fi,a[i].se=i;
sort(a+1,a+n+1);
pii sum={0,0},t1={0,0},t2={0,0};
for(int i=4;i<=n;++i)
{
pre[i]=sum.se;
pii temp={sum.fi+a[i].fi-a[i-1].fi, i};
sum=max(sum,t1);
t1=t2,t2=temp;
}
cout<<a[n].fi-a[1].fi-sum.fi<<" ";
int i=sum.se;
int v=n,cnt=0;
while(v>0)
{
int u=i;
cnt++;
for(;u<=v;u++)
out[a[u].se]=cnt;
v=i-1,i=pre[i];
}
cout<<cnt<<endl;
for(i=1;i<n;++i)
cout<<out[i]<<" ";
cout<<out[n]<<endl;
return 0;
}
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] 给你\( ...
- Codeforces1256E_Yet Another Division Into Teams
题意 n个人,每人有一个能力值a[i],要求分成多个队伍,每个队伍至少3个人,使得所有队伍的max(a[i])-min(a[i])之和最小. 分析 不会巧妙的dp,想了一天只想到了暴力的dp. 先排序 ...
- 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 ...
随机推荐
- nginx日志模块、事件模块
日志模块 1.access_log指令 语法: access_log path [format [buffer=size [flush=time]]]; access_log logs/access. ...
- 爆long long处理方法
#include <iostream> using namespace std; typedef __int128_t i128; typedef long long ll; int ma ...
- 怎样在浏览器端增加一条Cookie
可以使用 document.cookie, 这个属性可读可写, 读时是读取所有没有设置HttpOnly的cookie作为一个字符串返回, 写时是将一个cookie写入到document.cookie中 ...
- C# 7.0 语法
C# 7.0的语法主要是优化了之前的写法,使得更加简洁方便.try catch when 这个使用场景很少,正常的开发无业务处理的时候不建议使用 . #region 2.字符串嵌入值 Console ...
- Ubuntu18.04安装MySQL与默认编码设置
安装 打开终端直接开始,编码配置方法在后面 #通过apt更新包索引 sudo apt update #按照默认软件包安装 sudo apt install mysql-server #运行安全脚本 s ...
- java中数组的定义
1. 一维数组 int[] arr = new int[3];//需要一个容器,但是暂时不给具体的数值 int[] arr = new int[3]{1,2,3};//直接给定具体数值 int[] a ...
- Flutter 36: 图解自定义 View 之 Canvas (三)
小菜继续学习 Canvas 的相关方法: drawVertices 绘制顶点 小菜上次没有整理 drawVertices 的绘制方法,这次补上:Vertice 即顶点,通过绘制多个顶点,在进行连线,多 ...
- Django restfulframework 开发相关知识 整理
目录 目录 前言 前后端分离 实现前后端分离的方法 前后端分离带来的优点 RESTful十大规范 协议规范 域名规范 版本表示规范 url使用名词 http请求动词 过滤条件 状态码 错误信息 请求方 ...
- 第五章、Django之多表查询进阶与事务
目录 第五章.Django之多表查询 一.聚合查询 二.分组查询 三.F与Q查询 四.查询优化 五.Django开启事务 六.自定义char字段 七.ORM常用字段 第五章.Django之多表查询 一 ...
- Oracle笔记(十二) 集合、序列
一.集合 在数学的操作之中存在交.差.并.补的概念,而在数据的查询中也存在此概念,有如下几个连接符号: UNION:连接两个查询,相同的部分不显示: UNION ALL:连接两个查询,相同的部分显示: ...