zoukankan      html  css  js  c++  java
  • NOI2015 荷马史诗

    题目链接:戳我

    首先看出来这是一个哈夫曼树!

    然后就按照这里面哈夫曼树那一点说的,就可以A掉这个题啦

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<queue>
    #define MAXN 100010
    using namespace std;
    int n,k;
    long long ans;
    long long a[MAXN];
    struct Node
    {
        long long h,w;
        friend bool operator < (struct Node x,struct Node y)
        {
            if(x.w==y.w) return x.h>y.h;
            return x.w>y.w;
        }
    };
    priority_queue<Node>q;
    inline void solve()
    {
        while(q.size()>1)
        {
            long long cur_ans=0,maxx=0;
            for(int i=1;i<=k;i++)
            {
                cur_ans+=q.top().w;
                maxx=max(maxx,q.top().h);
                q.pop();
            }
            ans+=cur_ans;
            // printf("cur_ans=%lld maxx=%lld
    ",cur_ans,maxx);
            q.push((Node){maxx+1,cur_ans});
            // cout<<q.size()<<endl;
        }
    }
    int main()
    {
        #ifndef ONLINE_JUDGE
        freopen("ce.in","r",stdin);
        #endif
        scanf("%d%d",&n,&k);
        for(int i=1;i<=n;i++) scanf("%lld",&a[i]),q.push((Node){0,a[i]});
        int cur=1;
        while(cur<n) cur+=(k-1);
        for(int i=1;i<=cur-n;i++) q.push((Node){0,0});
        // printf("cur=%d
    ",cur);
        solve();
        printf("%lld
    ",ans);
        printf("%lld
    ",q.top().h);
        return 0;
    }
    
  • 相关阅读:
    十四
    十三
    十二
    十一
    用Linq从一个集合选取几列得到一个新的集合-可改列名
    LINQ入门(完结篇)
    LINQ入门(下篇)
    LINQ入门(中篇)
    LINQ入门(上篇)
    MVC中View往Controllers传数据的方式-已发
  • 原文地址:https://www.cnblogs.com/fengxunling/p/10947454.html
Copyright © 2011-2022 走看看