完全二叉树的权值
#include<iostream> using namespace std; typedef long long LL; const int N = 1e5 + 10; LL a[N]; int main() { int n; cin >> n; for (int i = 1; i <= n; i ++)cin >> a[i]; LL d = 1; LL mx = -1e18; LL dp; for (int i = 1, k = 1; i <= n; i += k, k *= 2, d++) { LL sum = 0; for (int j = k; j < k * 2 && j <= n; j ++) { sum += a[j]; } if (sum > mx) { dp = d; mx = sum; } } cout << dp << endl; }注意:最后一层可能不全,所以i要先加上k,k在乘2