zoukankan      html  css  js  c++  java
  • POJ 3253 Fence Repair

    题意:FJ要修栅栏,要用n段木板,每段长Li,现在FJ有一块长为∑Li的木板,要把这块木板分成n段,需要切割n-1次,每次的费用是当前木板长度,问最少要用多少费用。

    解法:利用哈弗曼树的思想贪心,用优先队列维护。每次取最小的两个木板,再将和放到优先队列中。

    代码:

    #include<stdio.h>
    #include<iostream>
    #include<algorithm>
    #include<string>
    #include<string.h>
    #include<math.h>
    #include<limits.h>
    #include<time.h>
    #include<stdlib.h>
    #include<map>
    #include<queue>
    #include<set>
    #include<stack>
    #include<vector>
    #define LL long long
    using namespace std;
    struct node
    {
        LL a;
        bool operator < (const node &tmp) const
        {
            return a > tmp.a;
        }
    };
    priority_queue <node> q;
    int main()
    {
        int n;
        while(~scanf("%d", &n))
        {
            while(!q.empty()) q.pop();
            LL x;
            for(int i = 0; i < n; i++)
            {
                scanf("%lld", &x);
                q.push((node){x});
            }
            LL ans = 0;
            while(q.size() > 1)
            {
                LL tmp = q.top().a;
                q.pop();
                tmp += q.top().a;
                q.pop();
                q.push((node){tmp});
                ans += tmp;
            }
            printf("%lld
    ", ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    spark java wordCount实例
    SpringMVC 商城项目
    jstl 遍历数据
    jquery 方法总结
    8ch
    7ch
    使用vuex实现组件间传值
    Vue父子组件之间传值
    从浏览器输入URL地址到最终页面渲染完成,发生了什么?
    数组去重
  • 原文地址:https://www.cnblogs.com/Apro/p/4810197.html
Copyright © 2011-2022 走看看