CodeForces-884D:Boxes And Balls(合并石子)
Ivan has n different boxes. The first of them contains some balls of n different colors.
Ivan wants to play a strange game. He wants to distribute the balls into boxes in such a way that for every i (1 ≤ i ≤ n) i-th box will contain all balls with color i.
In order to do this, Ivan will make some turns. Each turn he does the following:
- Ivan chooses any non-empty box and takes all balls from this box;
- Then Ivan chooses any k empty boxes (the box from the first step becomes empty, and Ivan is allowed to choose it), separates the balls he took on the previous step into k non-empty groups and puts each group into one of the boxes. He should put each group into a separate box. He can choose either k = 2 or k = 3.
The penalty of the turn is the number of balls Ivan takes from the box during the first step of the turn. And penalty of the game is the total penalty of turns made by Ivan until he distributes all balls to corresponding boxes.
Help Ivan to determine the minimum possible penalty of the game!
Input
The first line contains one integer number n (1 ≤ n ≤ 200000) — the number of boxes and colors.
The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 109), where aiis the number of balls with color i.
Output
Print one number — the minimum possible penalty of the game.
Example
3
1 2 3
6
4
2 3 4 5
19
题意:一共N小堆石子,每堆有自己的重量,开始都合在一大堆。现在问这样可以让他们分成N小堆。具体的,每次可以选择一个大的堆(其重量为代价),分为2或者3个堆。
思路:逆向考虑就是石子合并:若干堆石子,都有自己的重量,每一次可以选2或者3堆合成一堆,每次的代价为所选石子重量和。
对于此类题型,我们考虑到合并的次数是一定的,所以我们希望重量轻的其“凑次数”,即是把轻的优先合并,然后把合并的结果再拿去排序,等待下一次合并。
对于此题:显然能分为3个堆的情况优于分为2个堆(次数少一些),其他的与普通的合并石子无差。 然后就是考虑到每次减少2堆,最后要留1堆,所以对于偶数堆还应该插入“0”。
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
#define ll long long
const int maxn=;
priority_queue<ll,vector<ll>,greater<ll> >q;
ll a[maxn];
int main()
{
int N,i,j; ll ans=,tmp;
scanf("%d",&N);
for(i=;i<=N;i++) {
scanf("%lld",&a[i]);
q.push(a[i]);
}
if(N==) {
printf("0\n");
return ;
}
if(N%==) q.push();
while(q.size()>){
tmp=q.top(); q.pop();
tmp+=q.top(); q.pop();
tmp+=q.top(); q.pop();
ans+=tmp; q.push(tmp);
}
while(!q.empty()){
ans+=q.top(); q.pop();
}
printf("%lld\n",ans);
return ;
}
CodeForces-884D:Boxes And Balls(合并石子)的更多相关文章
- Educational Codeforces Round 31- D. Boxes And Balls
D. Boxes And Balls time limit per test2 seconds memory limit per test256 megabytes 题目链接:http://codef ...
- 【CF884D】Boxes And Balls 哈夫曼树
[CF884D]Boxes And Balls 题意:有n个箱子和若干个球,球的颜色也是1-n,有ai个球颜色为i,一开始所有的球都在1号箱子里,你每次可以进行如下操作: 选择1个箱子,将里面所有的球 ...
- UESTC 886 方老师金币堆 --合并石子DP
环状合并石子问题. 环状无非是第n个要和第1个相邻.可以复制该行石子到原来那行的右边即可达到目的. 定义:dp[i][j]代表从第i堆合并至第j堆所要消耗的最小体力. 转移方程:dp[i][j]=mi ...
- dp优化-四边形不等式(模板题:合并石子)
学习博客:https://blog.csdn.net/noiau/article/details/72514812 看了好久,这里整理一下证明 方程形式:dp(i,j)=min(dp(i,k)+dp( ...
- 合并石子(dp)
合并石子 时间限制: 1 Sec 内存限制: 128 MB提交: 7 解决: 7[提交][状态][讨论版][命题人:quanxing] 题目描述 在一个操场上一排地摆放着N堆石子.现要将石子有次序 ...
- Java实现 蓝桥杯 算法提高 合并石子
算法提高 合并石子 时间限制:2.0s 内存限制:256.0MB 问题描述 在一条直线上有n堆石子,每堆有一定的数量,每次可以将两堆相邻的石子合并,合并后放在两堆的中间位置,合并的费用为两堆石子的总数 ...
- Boxes And Balls(三叉哈夫曼编码)
题目 原题链接:http://codeforces.com/problemset/problem/884/D 现有一堆小石子,要求按要求的数目分成N堆,分别为a1.a2....an.具体的,每次选一个 ...
- CodeForces 958F3 Lightsabers (hard) 启发式合并/分治 多项式 FFT
原文链接http://www.cnblogs.com/zhouzhendong/p/8835443.html 题目传送门 - CodeForces 958F3 题意 有$n$个球,球有$m$种颜色,分 ...
- ny737 石子合并(一) 总结合并石子问题
描述: 在一个圆形操场的四周摆放着n 堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的2 堆石子合并成新的一堆,并将新的一堆石子数记为该次合并的得分. 试设计一个算法,计算出将n堆石子合并 ...
随机推荐
- elasticsearch入门使用(三) Query DSL
Elasticsearch Reference [6.2] » Query DSL 参考官方文档 :https://www.elastic.co/guide/en/elasticsearch/refe ...
- word2013 交叉引用添加参考文献的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,在进行“更新域”操作后,中括号消失。
word2013 交叉引用添加的尾注编号,通过查找 ^# 替换为 [^&] 的方式添加中括号,用这个方法添加中括号很多次了,这次却出现问题:在进行“更新域”操作后,中括号消失. 详 ...
- codevs——1297 硬币
1297 硬币 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 我们知道即使是同一种面值的硬币,它们的重量也 ...
- java实现简单的算法
排序大的分类可以分为两种:内排序和外排序.在排序过程中,全部记录存放在内存,则称为内排序,如果排序过程中需要使用外存,则称为外排序.下面讲的排序都是属于内排序. 内排序有可以分为以下几类: (1).插 ...
- VUE 自定义组件之间的相互通信
一.自定义组件 1.全局自定义组件 我们在var vm = new Vue({});的上面并列写上Vue.component('自定义组件名',{组件对象});来完成全局自定义组件的声明.示例代码如下 ...
- Java并发编程-Executor框架(转)
本文转自http://blog.csdn.net/chenchaofuck1/article/details/51606224 感谢作者 我们在传统多线程编程创建线程时,常常是创建一些Runnable ...
- 批量修改WORD表格属性
有时候需要对word中很多表格的属性进行修改,而word无法批量修改属性,所有这里记录一个宏 Sub TableFormatter() Dim oTbl As Table, i As Integer ...
- 前端高频面试题 JavaScript篇
以下问题都来自于互联网前端面经分享,回答为笔者通过查阅资料加上自身理解总结,不保证解答的准确性,有兴趣讨论的同学可以留言或者私信讨论. 1.JS的异步机制? 2.闭包如何实现? 3.原型链.继承? 4 ...
- scss - 语法
1.在一个样式导入另一个样式(@import "example.css") 2.scss单行注释不会显示出来 3.强大的变量(定义后,全局可使用) 4.全局默认变量(加!defau ...
- DesiredSize,RenderSize&& Width ,ActualWidth
做UI的时候刚入门,很多属性摸不着头脑,需要的功能和属性不能很快联系联想到,所以要慢慢积累UIElement 的DesiredSize 和 RenderSize UIElement 的DesiredS ...