8.3 第五场 Cute Tree

Cute Tree

*Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)

Total Submission(s): 1349 Accepted Submission(s): 876*

Problem Description

Given the pseudo-code of a function Build−Tree(A,id,L,R):
请添加图片描述

where A is given in input, id is the number of node, L ,R is the left position and the right position of A

Require the number of nodes created by Build−Tree(A,root,1,n).

Input

The first line contains an integer T (1≤T≤5) representing the number of test cases.

For each test case, the first contain one integer n(1≤n≤2∗105).

The second line contain n integers Ai(1≤Ai≤109).

Output

For each test output one line, the number of nodes created by Build−Tree(A,root,1,n).

Sample Input

样例1:

1
7
4 3 5 2 6 7 1

样例2:

2
4
2 2 5 3
10
21 10 5 89 12 3 42 13 55 76

Sample Output

样例1:

11

样例2:

6
15

大概题意:

给出建树逻辑,求节点数

思路:

按所给逻辑模拟即可

代码:

#include<iostream>
#include <string>
#include<cmath>

using namespace std;

const int N = 200010;
int n;
int A[N];
int tot = 0;

void buildtree(int L, int R) {
    tot = tot + 1;
    if (L == R)
        return;
    if (R - L == 1)
        tot += 2;
    else {
        int tp = R - L;
        int nn = tp / 3;
        int dd = tp % 3;
        if (dd != 0)
            nn++;
        int B = L + nn - 1;
        int C = (B + R) / 2;
        buildtree(L, B);
        buildtree(B + 1, C);
        buildtree(C + 1, R);
    }
}

int main() {
    ios::sync_with_stdio(false);
    int t;
    cin >> t;
    while (t--) {
        tot = 0;
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> A[i];
        buildtree(1, n);
        cout << tot << endl;
    }
}
posted @   嘿,抬头!  阅读(19)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示