荷马史诗

Solution

看到这道题,可以想到和合并果子类似,之后拓展成k叉合并果子就可以了,注意最下面一层不满时要补满

Code

复制代码
#include<bits/stdc++.h>
using namespace std;
#define max(a,b) (a>b?a:b)
typedef long long LL; 
struct node{
    LL w,h;
    node(LL W , LL H){w=W,h=H;}
};
bool operator < (node a,node b){//不知道为什么不能用大于
    return (a.w>b.w)||(a.w==b.w&&a.h>b.h);
}
priority_queue<node,vector<node>,less<node> > q;
int n,k,cnt;
LL x,t,mh,ans;
int main(){
    scanf("%d%d",&n,&k);
    for(int i=1;i<=n;i++)scanf("%lld",&x),q.push(node(x,1));
    if((n-1)%(k-1))cnt=k-1-(n-1)%(k-1);
    for(int i=1;i<=cnt;i++)q.push(node(0,1));
    cnt+=n;
    while(cnt>1){
        t=mh=0;
        for(int i=1;i<=k;i++){
            t+=q.top().w;
            mh=max(mh,q.top().h);
            q.pop();
        }
        ans+=t;
        q.push(node(t,mh+1));
        cnt-=k-1;
    }
    printf("%lld\n%lld\n",ans,q.top().h-1);
}
复制代码

 

posted @ 2019-08-26 22:13  Coder_cjh  阅读(224)  评论(0)    收藏  举报
编辑推荐:
· 源码浅析:SpringBoot main方法结束为什么程序不停止
· C#性能优化:为何 x * Math.Sqrt(x) 远胜 Math.Pow(x, 1.5)
· 本可避免的P1事故:Nginx变更导致网关请求均响应400
· 还在手写JSON调教大模型?.NET 9有新玩法
· 复杂业务系统线上问题排查过程
阅读排行:
· AI 的力量,开发者的翅膀:欢迎使用字节旗下的 AI 原生开发工具 TRAE
· 「闲聊文」准大三的我,思前想后还是不搞java了
· C#性能优化:为何 x * Math.Sqrt(x) 远胜 Math.Pow(x, 1.5)
· 千万级的大表如何新增字段?
· 《HelloGitHub》第 112 期
点击右上角即可分享
微信分享提示