二叉树算法题—— [蓝桥杯 2019 省 AB] 完全二叉树的权值

题目https://www.luogu.com.cn/problem/P8681

代码如下:

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;
#define fs first
#define sc second
#define endl '\n'
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef pair<int, int> PII;

const int N = 1e5;

int minh = -1;

int n;

int sum[N],a[N];

void dfs(int node,int h){
	if(node>n)return;
	
	sum[h]+=a[node];
	
	dfs(2*node,h+1);
	dfs(2*node+1,h+1);

    if(sum[h]>sum[minh]) minh = h;
}

int main(){
	
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);

    cin>>n;
    
    for(int i=1;i<=n;i++)cin>>a[i];
    
    dfs(1,1);
    
    cout<<minh<<endl;
    
    return 0;
}

测评结果:

相关推荐
罗西的思考2 小时前
【OpenClaw具身硬件】MiniClaw 阅读笔记---(1)基础
人工智能·算法·机器学习
蛋先生DX2 小时前
大模型参数存储格式揭秘:BF不是男朋友
深度学习·算法·llm
爱跳舞的烤冷面3 小时前
自学嵌入式第22天(数据结构——哈希)
数据结构·算法·哈希算法
猎嘤一号3 小时前
博弈论(Game Theory)的理论、算法与工程
人工智能·算法·安全·博弈论
月光船幽幽3 小时前
影子模式下保护 logits 不被修改
人工智能·python·算法
马拉AI3 小时前
腾讯开源 Agent 记忆系统,AI“换对话就忘”的问题有了新解法(附安装使用教程)
人工智能·算法·开源·科研
C++ 老炮儿的技术栈4 小时前
从 Qt Designer 属性编辑器的层级可以看到继承链
c语言·数据库·c++·qt·sqlite·visual studio
MC皮蛋侠客4 小时前
Redis 系列(一):全景与最小闭环——从 `SET` 命令到内存数据结构
数据结构·数据库·redis
疯狂打码的少年5 小时前
【数据结构】队列:定义、顺序队列与链式队列
数据结构·笔记
new_zhou5 小时前
C++ 项目 AI 协作指南(Windows / MSVC 环境)
c++·人工智能·windows