Codeforces Educational Codeforces Round 31
来源:互联网 发布:真实的苏秦知乎 编辑:程序博客网 时间:2024/06/06 13:29
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!
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 ai is the number of balls with color i.
Print one number — the minimum possible penalty of the game.
31 2 3
6
42 3 4 5
19
In the first example you take all the balls from the first box, choose k = 3 and sort all colors to corresponding boxes. Penalty is 6.
In the second example you make two turns:
- Take all the balls from the first box, choose k = 3, put balls of color 3 to the third box, of color 4 — to the fourth box and the rest put back into the first box. Penalty is 14;
- Take all the balls from the first box, choose k = 2, put balls of color 1 to the first box, of color 2 — to the second box. Penalty is 5.
Total penalty is 19.
题意:
- 有n种球,每种球有ai个
- 刚开始所有球都在第一个箱子里
- 每次操作就是从一个非空的箱子中拿出其中所有的球,并找来2或3个空箱子(刚刚将球全部拿出的也可以),将拿出的球分为任意非空的几组放入各自的箱子中
- Penalty是表示每次从箱子中拿出球的消耗,消耗等于拿出球的数量
- 求将所有n种球分别放入对应的n个箱子中最小的Penalty是多少
思路:
- 哈夫曼,逆向思维,从放好的状态推回到全部在同一个箱子的状态。
- 每次最好就是合三份,这样可以使得结果最小
- 满足每次合三份时,球的种类需要是3,5,7,9...
- 不满足时加入一个个数为0的种类来使其满足
#include<iostream>#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>#define LL long longusing namespace std;LL n,a,b,c,ans = 0;priority_queue<LL,vector<LL>,greater<LL> >q;int main(){ scanf("%lld",&n); for(int i=0;i<n;i++){ scanf("%lld",&a); q.push(a); } if(n==1){ printf("0\n"); return 0; } if(n%2==0) q.push(0); while(q.size()>1){ a = q.top();q.pop(); b = q.top();q.pop(); c = q.top();q.pop(); ans += a + b + c; q.push(a+b+c); } printf("%lld\n",ans); return 0;}
- Codeforces Educational Codeforces Round 31
- Codeforces Educational Codeforces Round 31
- Codeforces Educational Codeforces Round 31
- Codeforces Educational Codeforces Round 31
- Educational Codeforces Round 31
- Educational Codeforces Round 31
- Educational Codeforces Round 31
- Educational Codeforces Round 31
- Educational Codeforces Round 31 题解
- 【CODEFORCES】 Educational Codeforces Round 1
- Codeforces Educational Codeforces Round 5
- Codeforces Educational Codeforces Round 22
- Codeforces Educational Codeforces Round 27
- Codeforces Educational Codeforces Round 27
- Codeforces Educational Codeforces Round 27
- Codeforces Educational Codeforces Round 27
- Educational Codeforces Round 1
- Educational Codeforces Round 2
- 关于UILongPressGestureRecognizer调用多次的问题
- 程序运行时间
- 虚函数(C++)
- 用9path图片做背景 button和textview的文字不显示
- Windows下用QtCreator和C++写Android程序-上
- Codeforces Educational Codeforces Round 31
- git 获取不到新分支、切换到现有分支
- Spyder设置代码自动补全
- 嵌入式学习用什么编程语言
- Java-流程控制
- JAVA url类型的post请求示例
- 第八周项目4 稀疏矩阵的三元组表示的实现及应用(1)
- OpenGL 的空间变换(上):矩阵在空间几何中的应用
- java.sql.SQLException: 对只转发结果集的无效操作: last