洛谷$P2168 [NOI2015]$荷马史诗 贪心

正解:贪心

解题报告:

传送门$QwQ$

昂这个就哈夫曼树板子题鸭$QwQ$,只是从二叉变成多叉了$QwQ$

考虑用类似合并果子的方法?就从两个变成$k$个了嘛,一样的鸭,然后就做完了$QwQ$

注意一个细节趴$QwQ$,就可能存在选不满的情况,就先补0就成$QwQ$

$over$!

(然后记得开$ll$昂,,,$100pts->45pts$还是挺难受的了$kk$

 

#include<bits/stdc++.h>
using namespace std;
#define fi first
#define sc second
#define il inline
#define gc getchar()
#define mp make_pair
#define ll long long
#define int long long
#define P pair<ll,int>
#define ri register int
#define rb register bool
#define rc register char
#define rp(i,x,y) for(ri i=x;i<=y;++i)
#define my(i,x,y) for(ri i=x;i>=y;--i)
#define lb(x) lower_bound(st+1,st+st_cnt+1,x)-st

int n,K;
ll as;
priority_queue< P,vector<P>,greater<P> >Q;

il int read()
{
    rc ch=gc;ri x=0;rb y=1;
    while(ch!='-' && (ch>'9' || ch<'0'))ch=gc;
    if(ch=='-')ch=gc,y=0;
    while(ch>='0' && ch<='9')x=(x<<1)+(x<<3)+(ch^'0'),ch=gc;
    return y?x:-x;
}

signed main()
{
    //freopen("2168.in","r",stdin);freopen("2168.out","w",stdout);
    n=read();K=read();rp(i,1,n)Q.push(mp(read(),0));while((n-1)%(K-1))Q.push(mp(0,0)),++n;
    while((int)Q.size()>=K)
    {
        P nw=mp(0,0);
        rp(i,1,K)
        {
            P tmp=Q.top();Q.pop();nw.fi+=tmp.fi;nw.sc=max(nw.sc,tmp.sc+1);
        }
        Q.push(nw);as+=nw.fi;
    }
    printf("%lld
%lld
",as,Q.top().sc);
    return 0;
}
View Code

 

原文地址:https://www.cnblogs.com/lqsukida/p/11543729.html