神奇编码【牛客小白月赛78F】【队列的巧妙用法】

题目连接

  题意解读:有n个值,每个值,有b_{i}个。现在请你求出这样由\sum b_i个点组成的哈夫曼树的最小高度是多少?

  我们不难发现,对于哈夫曼过程不过是一个贪心过程,每次取两个值最小,在此条件下取树高最小的。那么,又可以发现,数量是不断的减少的,如果我们用三元组(value, num, deep)来表示以当前节点为根的哈夫曼树的根的权值、这样的树的数量、以及这棵树的高度的话,那么就会有,我们的num值是在以不断除以2这样的方式递减的,放在小根堆中的元素的数量也只有n,于是时间复杂度就是O(n \times log_2 n \times log_2 num),这样的复杂度的运算次数大概是T(10^5 \times 20 \times 40) \approx T(1e8),而这样的量级,在本题中会被无情的卡掉,因为还有一定的常数复杂度。

  所以,我们要优化掉一个log_2

  那么,这要求我们抓住其中的线性关系,不难发现,其中的线性关系在于权值和。制造哈夫曼树的过程,有一个很容易被我们忽视掉的线性关系,就是哈夫曼树的新生成的根节点,他们的权值是不断的递增的。

  这样,我们只需要再维护一个合成的节点的信息就可以了。

  所以,可以对没合成的节点做一个权值升序处理(题目的输入就是权值顺序,所以直接一个个的插入即可);再者,就是维护合成之后的节点,因为哈夫曼树的性质,我们可以将这些节点逐一的放入到“合成后的节点”的小根堆中去,由于这里是线性递增的,所以这个“小根堆”我们可以使用队列去进行维护。

  具体的操作如下所示:

1、取出一个值最小的节点,与这个节点相同的节点数量大于1,直接自己跟自己合并,如果数量是奇数,则最后会多余一个,将这个重新送回至队首(所以需要双端队列)。

2、取出一个值最小的节点,它只有一个,那么我们还需要再取一个,然后跟他合并,新取出来的节点的数量如果大于1,那么还需要减掉这个使用掉的,再把自己送回自己的队列中去。

3、最后只剩一个节点,那么它的高度就是答案了。

#include <bits/stdc++.h>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define pii pair<int, int>
#define MP(a, b) make_pair(a, b)
using namespace std;
typedef unsigned long long ull;
typedef unsigned int uit;
typedef long long ll;
struct node {
    __int128 val; ll num, deep;
    node(__int128 a = 0, ll b = 0, ll c = 0):val(a), num(b), deep(c) {}
    friend bool operator < (node e1, node e2) {
        return e1.val == e2.val ? e1.deep < e2.deep : e1.val < e2.val;
    }
    friend bool operator == (node e1, node e2) {
        return e1.val == e2.val && e1.deep == e2.deep;
    }
};
struct Heap {
    deque<node> Q[2];
    int qid;
    Heap() {
        while(!Q[0].empty()) Q[0].pop_front();
        while(!Q[1].empty()) Q[1].pop_front();
        qid = 0;
    }
    void clear() {
        while(!Q[0].empty()) Q[0].pop_front();
        while(!Q[1].empty()) Q[1].pop_front();
        qid = 0;
    }
    inline bool empty() { return Q[0].empty() && Q[1].empty(); }
    inline ll Size() {
        ll siz = Q[0].size() + Q[1].size();
        if(siz > 1) return siz;
        int id = Q[0].empty();
        if(Q[id].front().num == 1) return 1;
        else return 2;
    }
    inline node Top() {
        if(Q[0].empty() || Q[1].empty()) qid = Q[0].empty();
        else if(Q[0].front() < Q[1].front()) qid = 0;
        else qid = 1;
        return Q[qid].front();
    }
    inline void Pop() {
        Q[qid].pop_front();
    }
} Que;
int n;
int main()
{
    scanf("%d", &n);
    Que.clear();
    for(int i = 1; i <= n; i ++) {
        ll b;
        scanf("%lld", &b);
        if(b) Que.Q[0].push_back(node(i, b, 0));
    }
    while(Que.Size() > 1) {
        node now = Que.Top(), merge;
        Que.Pop();
        if(now.num > 1) {
            if(now.num & 1) {
                Que.Q[Que.qid].push_front(node(now.val, 1, now.deep));
            }
            merge = node(now.val << 1LL, now.num >> 1LL, now.deep + 1);
            Que.Q[1].push_back(merge);
        }
        else {
            node nex = Que.Top();
            Que.Pop();
            nex.num --;
            if(nex.num) Que.Q[Que.qid].push_front(nex);
            merge = node(now.val + nex.val, 1, max(now.deep, nex.deep) + 1);
            Que.Q[1].push_back(merge);
        }
    }
    printf("%lld\n", Que.Top().deep);
    return 0;
}

### 关于牛客小白109的信息 目前并未找到关于牛客小白109的具体比信息或题解内容[^5]。然而,可以推测该事可能属于牛客网举办的系列算法之一,通常这类比会涉及数据结构、动态规划、图论等经典算法问题。 如果要准备类似的事,可以通过分析其他场次的比题目来提升自己的能力。例如,在牛客小白13中,有一道与二叉树相关的题目,其核心在于处理树的操作以及统计最终的结果[^3]。通过研究此类问题的解决方法,能够帮助理解如何高效地设计算法并优化时间复杂度。 以下是基于已有经验的一个通用解决方案框架用于应对类似场景下的批量更新操作: ```python class TreeNode: def __init__(self, id): self.id = id self.weight = 0 self.children = [] def build_tree(n): nodes = [TreeNode(i) for i in range(1, n + 1)] for node in nodes: if 2 * node.id <= n: node.children.append(nodes[2 * node.id - 1]) if 2 * node.id + 1 <= n: node.children.append(nodes[2 * node.id]) return nodes[0] def apply_operations(root, operations, m): from collections import defaultdict counts = defaultdict(int) def update_subtree(node, delta): stack = [node] while stack: current = stack.pop() current.weight += delta counts[current.weight] += 1 for child in current.children: stack.append(child) def exclude_subtree(node, total_nodes, delta): nonlocal root stack = [(root, False)] # (current_node, visited) subtree_size = set() while stack: current, visited = stack.pop() if not visited and current != node: stack.append((current, True)) for child in current.children: stack.append((child, False)) elif visited or current == node: if current != node: subtree_size.add(current.id) all_ids = {i for i in range(1, total_nodes + 1)} outside_ids = all_ids.difference(subtree_size.union({node.id})) for idx in outside_ids: nodes[idx].weight += delta counts[nodes[idx].weight] += 1 global nodes nodes = {} queue = [root] while queue: curr = queue.pop(0) nodes[curr.id] = curr for c in curr.children: queue.append(c) for operation in operations: op_type, x = operation.split(' ') x = int(x) target_node = nodes.get(x, None) if not target_node: continue if op_type == '1': update_subtree(target_node, 1) elif op_type == '2' and target_node is not None: exclude_subtree(target_node, n, 1) elif op_type == '3': path_to_root = [] temp = target_node while temp: path_to_root.append(temp) if temp.id % 2 == 0: parent_id = temp.id // 2 else: parent_id = (temp.id - 1) // 2 if parent_id >= 1: temp = nodes[parent_id] else: break for p in path_to_root: p.weight += 1 counts[p.weight] += 1 elif op_type == '4': pass # Implement similarly to other cases. result = [counts[i] for i in range(m + 1)] return result ``` 上述代码片段展示了针对特定类型的树形结构及其操作的一种实现方式。尽管它并非直接对应小白109中的具体题目,但它提供了一个可借鉴的设计思路。 ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wuliwuliii

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值