Educational Codeforces Round 31 - D. Boxes And Balls (哈夫曼树)

本文介绍了一个关于3叉哈夫曼树的算法问题,目标是最小化分配不同颜色球到对应盒子过程中的总代价。通过优先队列实现有效求解,并允许在必要时添加虚拟节点。
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 ≤ ni-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:

  1. Ivan chooses any non-empty box and takes all balls from this box; 
  2. 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 a1a2, ..., an (1 ≤ ai ≤ 109), where ai is the number of balls with color i.

Output

Print one number — the minimum possible penalty of the game.

Examples
input
3
1 2 3
output
6
input
4
2 3 4 5
output
19
Note

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: 

  1. 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
  2. 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.



题意:
即3叉的哈夫曼树。

POINT:
k叉的哈夫曼树,可以添加0的点,添加至 数量%(k-1)==1时,即可。

#include <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
#define LL long long
const LL maxn = 100003;
priority_queue<LL,vector<LL>,greater<LL> >q;
int main()
{
    LL n;scanf("%lld",&n);
    for(LL i=1;i<=n;i++){
        LL x;scanf("%lld",&x);
        q.push(x);
    }
    if(n%2==0) q.push(0);
    LL ans=0;
    while(q.size()>1){
        LL a=q.top();q.pop();
        LL b=q.top();q.pop();
        LL c=q.top();q.pop();
        ans+=a+b+c;
        q.push(a+b+c);
    }
    printf("%lld\n",ans);
    
    return 0;
}


对于Codeforces Round 1005 Div. 2中的D题解答或解释,当前提供的引用资料并未直接涉及该轮次的比赛题目详情。然而,可以基于过往比赛的经验以及相似类型的编程竞赛问题提供一般性的指导。 ### 解决方案概述 通常情况下,在解决此类算法竞赛题目时,会遵循特定的方法论来处理输入数据并计算所需的结果。虽然具体到Codeforces Round 1005 Div. 2 Problem D的信息未被提及,但可以根据以往经验推测可能涉及到的数据结构和算法技术: - **读取测试案例数量**:程序首先应该能够接收多个独立的测试案例数目\(t\),其中每一个案例都包含了不同的参数集[^3]。 - **解析数组元素**:针对每个测试案例,需解析给定长度为\(n\)的一系列整数\[a_1, a_2,...,a_n\]作为操作对象[^2]。 - **查询次数限制**:需要注意的是,所有测试案例中查询总数不得超过设定的最大值,比如这里提到不超过\(2 \times 10^5\)次查询[^1]。 - **输出格式规定**:当准备打印最终答案时,应按照指定格式输出结果,并继续处理下一个测试案例直到完成全部测试[^4]。 考虑到这些通用原则,如果要设计一个适用于此类型问题的解决方案框架,则可能会如下所示: ```python def solve_problem(): import sys input = sys.stdin.read data = input().split() index = 0 results = [] t = int(data[index]) index += 1 for _ in range(t): n = int(data[index]) index += 1 numbers = list(map(int, data[index:index+n])) index += n # 假设这里是解决问题的核心逻辑部分, # 需要根据具体的Problem D描述调整这部分代码实现。 result_for_case = "Example Result" results.append(result_for_case) print("\n".join(results)) ``` 上述伪代码展示了如何构建基本架构用于批量处理多组测试用例,但是核心业务逻辑需要依据实际问题定义进行填充和完善。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值