Fork me on GitHub

acwing1240. 完全二叉树的权值

ACWING1240. 完全二叉树的权值

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
typedef long long LL;
using namespace std;
const int maxn = 1e5 + 10;
LL n;
int a[maxn];
vector<pair<LL, int>> ans;  // 结果,层数

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i ++ )
    {
        scanf("%d", &a[i]);
    }
    int base = 1, level = 1;
    while (base <= n)
    {
        // cout << level << endl;
        LL temp = 0;
        int end = pow(2, level) - 1;
        if (end > n) end = n; 
        for (int i = base; i <= end; i ++ )
        {
            temp += a[i];
        }
        ans.push_back({temp, level});
        level ++;
        base = pow(2, level -1);    
    }
    sort(ans.begin(), ans.end());
    cout << ans.back().second <<endl;
    return 0;
}

记录下来每一层的值的信息,然后排序

如果加的数量都是很大的话,需要用到long long

posted @ 2020-08-17 23:12  WalterJ726  阅读(170)  评论(0编辑  收藏  举报