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 ...
随机推荐
- Java 并发进阶常见面试题总结
声明:本文内容完全来自网络,转自GitHub->JavaGuide(https://github.com/Snailclimb/JavaGuide),致谢 1. synchronize ...
- python matplotlib 折线图
1.绘制折线图,去上和右边框,显示中文 import numpy as np import matplotlib.pyplot as plt #plt.style.use('default') #pl ...
- javaIO——BufferedWriter
[环境] jdk1.8 前面学习过 BufferedReader,是缓冲字符输入流.那么今天来学习对应的缓冲字符输出流类:BufferedWriter.跟 BufferedReader 同理,它也是一 ...
- 【CNN】 吴恩达课程中几种网络的比较
LeNet5 AlexNet VGG16 ResNet : 残差网络 Inception Net : 特点,可以通过1*1*192 的卷积核来缩减参数个数,压缩维度,约缩减10倍, 例如 :用1 ...
- WinPE基础知识之重定位表
typedef struct _IMAGE_BASE_RELOCATION { DWORD VirtualAddress; // (重要)需要重定位的位置,这是一个RVA DWORD SizeOfBl ...
- 根据CPU核心数确定线程池并发线程数(转)
一.抛出问题 关于如何计算并发线程数,一般分两派,来自两本书,且都是好书,到底哪个是对的?问题追踪后,整理如下: 第一派:<Java Concurrency in Practice>即&l ...
- 如何从零搭建hexo个人博客网站
https://www.jianshu.com/p/adf65cbad393?utm_source=oschina-app 准备工作 github账号 node.js 环境搭建 git使用 mar ...
- rabbimq 生产消费者
composer.json { "require": { "php-amqplib/php-amqplib": "^2.9" } } com ...
- Mysql(三):表操作
一 存储引擎介绍 存储引擎即表类型,mysql根据不同的表类型会有不同的处理机制 详见:http://www.cnblogs.com/6324TV/p/8481061.html 二 表介绍 表相当于文 ...
- H2数据库启动提示8082端口被占用
The Web Console server could not be started. Possible cause: another server is already running at ht ...