zoukankan      html  css  js  c++  java
  • POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln。每次切断木板的花费为这块板的长度,问最小花费。21 分为 5 8 8三部分。
     
    思路:思考将n部分进行n-1次两两合成最终合成L长度和题目所求花费一致。贪心,按木板长度排序,每次取长度最小的两块木板,则答案最小。因为合成次数是固定不变的,尽量让小的部分进行多次合成,这样总花费最小。
     
    #include<cstdio>
    #include<queue>
    using namespace std;
    typedef long long ll;
    priority_queue<int,vector<int>,greater<int> > que;
    int main() {
        int n,x;
        while(~scanf("%d",&n)) {
            while(!que.empty()) que.pop();
            for(int i=0;i<n;i++) {
                scanf("%d",&x);
                que.push(x);
            }
            int a,b;ll res=0;
            while(que.size()>1) {
                a=que.top();que.pop();
                b=que.top();que.pop();
                res+=a+b;
                que.push(a+b);
            }
            printf("%lld
    ",res);
        }
        return 0; 
    } 
  • 相关阅读:
    Steady Cow Assignment POJ
    二分图多重匹配问题
    Tour HDU
    奔小康赚大钱 HDU
    Strategic Game HDU
    Antenna Placement POJ
    Oil Skimming HDU
    Rain on your Parade HDU
    假如,
    这样也可以,insert,,
  • 原文地址:https://www.cnblogs.com/LinesYao/p/5740863.html
Copyright © 2011-2022 走看看