poj3253 Fence Repair

来源:互联网 发布:如何建立网络 编辑:程序博客网 时间:2024/06/06 02:45


Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needsN (1 ≤ N ≤ 20,000) planks of wood, each having some integer lengthLi (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into theN planks (i.e., whose length is the sum of the lengths Li). FJ is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; you should ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to Farmer Don's Farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of theN-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets Farmer John decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create theN planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input
Line 1: One integer N, the number of planks
Lines 2.. N+1: Each line contains a single integer describing the length of a needed plank
Output
Line 1: One integer: the minimum amount of money he must spend to makeN-1 cuts
Sample Input
3858
Sample Output
34
Hint
He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.
The original board measures 8+5+8=21. The first cut will cost 21, and should be used to cut the board into pieces measuring 13 and 8. The second cut will cost 13, and should be used to cut the 13 into 8 and 5. This would cost 21+13=34. If the 21 was cut into 16 and 5 instead, the second cut would cost 16 for a total of 37 (which is more than 34).
给你一个无限长的木板,你需要锯成所给的长度,费用是与长度相等,问所需要的最小长度,完全转化为哈夫曼树嘛!!

ac代码:

#include<stdio.h>
#include <cstring>
#include <iostream>
#include <queue>
using namespacestd;
struct cmp1{ booloperator ()(longlong &a,long long &b){
        return a>b;
    }
};
int main(){
   long long n,m;
   while(cin>>n)
   {
       priority_queue<long long,vector<longlong>,cmp1> que;
       for(int i=1;i<=n;i++)
       {
           scanf("%lld",&m);
           que.push(m);
       }
        long long re=0;
        while(!que.empty())
        {
            long long sum=0;
            sum+=que.top();
            que.pop();
            if(!que.empty())
            {
                sum+=que.top();
                que.pop();
                if(!que.empty())
                que.push(sum);
            }
            re+=sum;
        }
        cout<<re<<endl;
   }
   return 0;
}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 误给宝宝吃了坏的饭怎么办 鼠标没反应键盘指示灯不亮怎么办 新买变频冰箱风机声音大怎么办 三星手机玩王者荣耀一直闪退怎么办 刚申请的阿里大宝卡不想要了怎么办 国美刚买不到一个月电视坏了怎么办 交保险后保险公司不给开收据怎么办 收据白联作废红联丢失怎么办 收据作废客户联给客户了怎么办 宜家买的床和床垫搬家了怎么办 科龙空调开机后自己关机怎么办 以旧换新旧的没给商家 报案怎么办 想换新手机但是旧的没坏怎么办 从苏宁易购买的电视坏了怎么办 苏宁易购服务站买的电视坏了怎么办 用微信登陆京东账号退不出来怎么办 微信账号密码手机号都忘记了怎么办 我的手机号京东被别人绑定了怎么办 京东之前绑定的手机号不用了怎么办 京东退货钱未到银行卡账号里怎么办 京东身份证绑的手机号不用了怎么办 海尔冰箱要退货箱子扔了怎么办 海尔冰箱门变形关不严没吸力怎么办 长时间不用的手机忘记密码怎么办啊 微信忘记密码手机号又不用了怎么办 华为账号换了手机密码忘记了怎么办 用u盘制作音响喊话内容怎么办 新三板公司退市了小股东怎么办? 新三板公司退市股东股票怎么办 利群收购乐天玛特超市卡怎么办 买房过户夫妻一方是外省户口怎么办 欠了国美金融贷款没钱还会怎么办? 国企员工涨工资不在编的员工怎么办 装车牌照的螺丝孔小了怎么办 北京摇号之后中签和未中签怎么办 拉轿车的大车出车祸车怎么办 美图m6手机忘记锁屏密码怎么办 微信验证码登录收不到验证码怎么办 微信被限制登录收不到验证码怎么办 微信登录申诉收不到短信怎么办 登录微信手机收不到验证码怎么办