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 ...
随机推荐
- Centos7 部署.net core2.1 详细步骤
安装dotnet sdk(添加产品秘钥与yum源) 添加yum源:sudo rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages ...
- wepy2创建项目
1.首先 在桌面(自己选定目录下)新建一个文件夹,注意需要使用英文名. 2.Cmd中 进入到该文件目录下 3.安装 wepy 命令行工具. npm install wepy-cli -g wepy ...
- 通过ABAP代码判断当前系统类型,BYD还是S4 OP还是S4 Cloud
用工具类 CL_COS_UTILITIES IS_BYD 如果是BYD系统,这个方法的实现会硬编码返回一个true, 在其他系统里则返回false,如图: IS_SUITE 原理同上,suite系统里 ...
- js页面内容只读,不可复制
// document.oncontextmenu=new Function("event.returnValue=false"); // document.onselectsta ...
- IntelliJ IDEA安装及破解
百度搜索IntelliJ IDEA,进入官网. 下载完成后进入安装界面 根据自己的情况选择安装路径 等待下载和安装完成. 安装完成 接下来我们运行IntelliJ IDEA 之后这里就要我们进行激活了 ...
- 【异常】 Could not find Linker 'g++' in system path.
1 详细异常 FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':az-ex ...
- linux基础5-vi文本处理器
三种模式下各自可以完成的操作: 一般模式:可以完成光标移动.删除单个和整行字.复制和黏贴,通过i.o.a.r这几个命令进入编辑模式 编辑模式:可以输入字符,通过esc返回一般模式 指令模式:读取文件, ...
- Oralce问题之ORA-12560:TNS协议适配器错误
在Windows系统中,通过CMD命令打开命令窗口,通过命令:sqlplus / as sysdba回车后提示 协议适配器错误 可能原因: (1).Oralce数据库监听服务没启动起来 通过开始-&g ...
- 【Codeforces】Orz Panda Cup
大大出的题 大大经常吐槽没有人补,所以我决定做一个 A. APA of Orz Pandas 题意:给你一个包含+-*/%和()的表达式,让你把它转化成java里BigInteger的形式 大概就像这 ...
- Python 获得程序 exe 的版本号
Python 获得程序 exe 的版本号 python中需要安装 pywin32 包 # based on http://stackoverflow.com/questions/580924/pyth ...