F. 0, 1, 2, Tree!

原题链接

题解

1.模拟+贪心,我们一个一个点添加,一层一层遍历,每个节点对当前层的接口数的贡献是-1
如果是节点2,对下一层接口数贡献为2,节点1贡献为1
如果当前层接口数用完了就下一层,初始值层0设为1

在时间复杂度合理的情况下无所不用其极

code

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll tree[200005]={0};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c;
        cin>>a>>b>>c;
        if(a*2+b+1!=a+b+c)
        {
            puts("-1");
            continue;
        }

        ll x=a,y=b;
        ll height=0;
        tree[0]=1;
        for(int i=1;i<=a+b+c;i++)
        {
            if(!tree[height]) height++;
            tree[height]--;
            if(x)
            {
                x--;
                tree[height+1]+=2;
            }
            else
            {
                y--;
                tree[height+1]++;
            }
        }
        cout<<height<<endl;
        memset(tree,0,sizeof tree);
    }
    return 0;
}

posted @   纯粹的  阅读(84)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示