1 条题解

  • 2
    @ 2023-2-27 20:53:38
    #include<iostream>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef pair<long long ,int > PII;
    priority_queue<PII,vector<PII>,greater<PII>> head;//建立一个小根堆
    int main()
    {
        int n,k;
        cin>>n>>k;
        for(int i=0;i<n;i++)
        {
            long long x;
            cin>>x;
            head.push({x,0});//两个值一个存储出现的次数,存储节点的深度
        }
        while((n-1)%(k-1))//因为这是k叉树,如果n%k!=0,这样求出来的是错误的,所以要添加0来凑数
        head.push({0,0}),n++;
        long long sum=0;//记录的是总的代价
        while(head.size()>1)
        {
            long long sum1=0;//记录的是当前一棵树的k个分支的代价
            int depth=0;
            for(int i=0;i<k;i++)
            {
                auto t=head.top();
                
                
                sum1+=t.first;
                depth=max(depth,t.second);
                head.pop();
            }
            sum+=sum1;
            head.push({sum1,depth+1});
        }
        cout<<sum<<endl<<head.top().second<<endl;
        return 0;
    }
    
    
    • 1

    信息

    ID
    60
    时间
    1000ms
    内存
    128MiB
    难度
    1
    标签
    递交数
    40
    已通过
    34
    上传者