第十届蓝桥杯 JavaA 完全二叉树的权值

第十届蓝桥杯 JavaA 完全二叉树的权值_第1张图片

第十届蓝桥杯 JavaA 完全二叉树的权值_第2张图片

 

import java.util.Scanner;

public class Main {
	static Scanner in = new Scanner(System.in);
	static int n;
	static long[] a = new long[100009];
	static long[] sum = new long[100009];
	static int minDepth = Integer.MAX_VALUE;
	static long maxSum = Integer.MIN_VALUE;
	static boolean flag;

	public static void main(String[] args) {
		n = in.nextInt();
		for (int i = 1; i <= n; ++i)
			a[i] = in.nextLong() + a[i - 1];
		
		for (int i = 1; i <= 100009; ++i) {
			int l = (int) Math.pow(2, i - 1);
			int r = (int) Math.pow(2, i) - 1;
			if (n <= r) {
				r = n;
				flag = true;
			}

			sum[i] = a[r] - a[l - 1];

			if (sum[i] > maxSum) {
				maxSum = sum[i];
				minDepth = i;
			}
			if (flag)
				break;
		}
		System.out.println(minDepth);
	}
}

 

你可能感兴趣的:(第十届蓝桥杯 JavaA 完全二叉树的权值)