y总找规律+双指针
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e5 + 10;
int n;
int a[N];
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
LL maxs = -1e18;
int depth = 0;
for (int d = 1,i = 1; i <= n; i *= 2, d++) {
LL s = 0;
for (int j = i; j < i + (1 << d - 1) && j <= n; j++)
s += a[j];
if(s>maxs){
maxs = s;
depth = d;
}
}
cout << depth << endl;
return 0;
}
自己写的 根据二叉树的性质标记深度
#include<bits/stdc++.h>
using namespace std;
int n;
const int N = 1e6 + 10;
int a[N];
pair<int, int> tr[N];
long long ans = -0x3f3f3f3f;
int ansdeep = 0;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
tr[1].second = a[1];
tr[1].first = 1;
ans = a[1];
ansdeep = 1;
for (int i = 1; i <= n; i++) {
tr[2 * i].second = a[2 * i];
tr[2 * i].first = tr[i].first + 1;
tr[2 * i + 1].second = a[2 * i + 1];
tr[2 * i + 1].first = tr[i].first + 1;
}
sort(tr + 1, tr + n + 1);
//for (int i = 1; i <= n; i++) {
// cout << tr[i].first << ' ' << tr[i].second<<endl;
//}
for (int i = 1; i <= n; ) {
long long sum = 0;
int nowdeep = tr[i].first;
while (tr[i].first == nowdeep) {
sum += tr[i].second;
i++;
}
//cout << sum << ' ';
if (sum > ans) {
ansdeep = nowdeep;
}
ans = max(ans, sum);
}
cout << ansdeep << endl;
return 0;
}