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

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);
	}
}
Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐