荷馬史詩----------------------------思維(k叉哈夫曼樹)

在這裏插入圖片描述
在這裏插入圖片描述

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second
typedef long long ll;
typedef pair<ll,int> PII;
int n,k;
int main()
{
    cin>>n>>k;
    ll x;
    priority_queue<PII,vector<PII>,greater<PII> > q;
    for(int i=1;i<=n;i++)
    {
        cin>>x;
        q.push({x,0});
    }
    while((n-1)%(k-1)!=0) q.push({0,0}),n++;
    ll res=0;
    int depth=0;
    while(q.size()>1)
    {
        ll s=0;
        for(int i=0;i<k;i++ )
        {
            auto t=q.top();
            q.pop();
            s+=t.x;
            depth=max(depth,t.y);
           
        }
        res+=s; q.push({s,depth+1});
    }
    cout<<res<<endl<<q.top().y<<endl;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章