蓝桥杯(3.17 刷真题)

1240. 完全二叉树的权值
P8681 [蓝桥杯 2019 省 AB] 完全二叉树的权值
AC

java 复制代码
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] res = new int[n+1];
		for(int i=1;i<=n;i++) {
			res[i] = sc.nextInt();
		}
		int cs = 1;
		long MAX = Long.MIN_VALUE;
		int key = 0;
		int t = 1;
		for(int i=1;i<=n;i*=2,cs++,t*=2) {
			int w = t;
			long sum = 0;
			for(int j=i;j<=n&&t!=0;j++,t--) {
				sum+=res[j];
			}
			if(sum>MAX) {
				MAX = sum;
				key = cs;
			}
			t = w;
		}
		System.out.println(key);
	}
}
相关推荐
Accerlator3 小时前
2026 年 4 月 1 日电话面试
面试·职场和发展
凌波粒10 小时前
LeetCode--344.反转字符串(字符串/双指针法)
算法·leetcode·职场和发展
啊哦呃咦唔鱼10 小时前
LeetCode hot100-543 二叉树的直径
算法·leetcode·职场和发展
x_xbx13 小时前
LeetCode:438. 找到字符串中所有字母异位词
算法·leetcode·职场和发展
List<String> error_P13 小时前
蓝桥杯最后几天冲刺
蓝桥杯
阿Y加油吧14 小时前
LeetCode 中等难度 | 回溯法进阶题解:单词搜索 & 分割回文串
算法·leetcode·职场和发展
Tanecious.14 小时前
蓝桥杯备赛:Day8-小苯的异或和
c++·蓝桥杯
田梓燊17 小时前
leetcode 73
算法·leetcode·职场和发展
南风知我意95717 小时前
【重构思维】用位运算做权限管理
前端·面试·职场和发展·性能优化·重构
黄昏回响17 小时前
计算机系统基础知识(十四):软件篇之计算机网络详解
计算机网络·程序人生·面试·职场和发展·改行学it