题目链接:http://poj.org/problem?id=3253

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of wood, each having some integer length Li (1 ≤ Li ≤ 50,000) units. He then purchases a single long board just long enough to saw into the N 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 the N-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 the N 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 make N-1 cuts

Sample Input

3
8
5
8

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).
题意:有一个农夫要把一个木板钜成几块给定长度的小木板,每次锯都要收取一定费用,这个费用就是当前锯的这个木版的长度

给定各个要求的小木板的长度,及小木板的个数n,求最小费用

提示:

3

8

8

5为例:

先从无限长的木板上锯下长度为 21 的木板,花费 21

再从长度为21的木板上锯下长度为5的木板,花费5

再从长度为16的木板上锯下长度为8的木板,花费8

总花费 = 21+5+8 =34

解题思路:由于木板的切割顺序不确定,自由度很高,这个题目貌似很难入手。但是其实可以用略微奇特的贪心法来求解。利用Huffman思想,要使总费用最小,那么每次只选取最小长度的两块木板相加,再把这些“和”累加到总费用中即可。

第一种做法:

附上代码:

 #include<iostream>
using namespace std;
typedef long long ll;
int l[];
int main()
{
int n;
while(cin>>n)
{
ll ans=;
for(int i=;i<n;i++) cin>>l[i];
//直到计算到木板为1是为止
while(n>)
{
//求出最短的木板x和次短的木板 y
int x=,y=;
if(l[x]>l[y]) swap(x,y);
for(int i=;i<n;i++)
{
if(l[i]<l[x]){
y=x;
x=i;
}
else if(l[i]<l[y]){
y=i;
}
}
//将两块最短的板合并
int t=l[x]+l[y];
ans+=t;
if(x==n-) swap(x,y);
l[x]=t;
l[y]=l[n-];
n--;
}
cout<<ans<<endl;
}
return ;
}

第二种做法:采用优先队列:定义一个从小到大排列的优先队列,每次取出队首的两个元素,并将它们的和压入优先队列,答案累加记录它们的和,直到优先队列剩下最后一个元素即可。

附上代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
priority_queue<int,vector<int>,greater<int> > que;
ll n,ans; int main()
{
cin>>n;
ll l;
for(int i=;i<n;i++)
{
cin>>l;
que.push(l);
}
while(que.size()>)
{
ll x=que.top();
que.pop();
ll y=que.top();
que.pop();
ans+=x+y;
que.push(x+y);
}
cout<<ans<<endl;
return ;
}

Fence Repair(poj3253)的更多相关文章

  1. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  2. POJ 3253 Fence Repair (贪心)

    题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切 ...

  3. POJ 3253 Fence Repair(修篱笆)

    POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS   Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...

  4. Greedy:Fence Repair(POJ 3252)

    Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...

  5. poj 3253:Fence Repair(堆排序应用)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23913   Accepted: 7595 Des ...

  6. poj 3253 Fence Repair (STL优先队列)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...

  7. POJ 3253 Fence Repair(哈夫曼树)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26167   Accepted: 8459 Des ...

  8. Fence Repair (二叉树求解)(优先队列,先取出小的)

    题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Sub ...

  9. [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 25274   Accepted: 8131 Des ...

随机推荐

  1. 微信小程序:java后台获取openId

    一.功能描述 openId是某个微信账户对应某个小程序或者公众号的唯一标识,但openId必须经过后台解密才能获取(之前实现过前台解密,可是由于微信小程序的种种限制,前台解密无法在小程序发布后使用) ...

  2. [JSOI2016]病毒感染[dp]

    题意 有 \(n​\) 个村庄按标号排列,每个村庄有一个死亡速度 \(a_i​\) 表示每天死 \(a_i​\) 人(除非你治好这个村庄). 你从 1 号村庄出发,每天可以选择向相邻的村庄进发或者治愈 ...

  3. pycharm2019注册码一键实时获取,永久有效!

    pycharm2019专业版激活码 56ZS5PQ1RF-eyJsaWNlbnNlSWQiOiI1NlpTNVBRMVJGIiwibGljZW5zZWVOYW1lIjoi5q2j54mI5o6I5p2 ...

  4. Jenkins持续集成构建自动化测试项目

    1.配置内容安全策略(配置一次): 1.1.构建一个自由风格的软件项目 1.2.Build Triggers:Build when job nodes start 1.3.Build:Execute ...

  5. phpstorm 报错及解决

    1. 当项目里有大量 js 文件时,一旦编辑包含 js 的文件,phpstorm 会卡顿,甚至未响应 问题原因: 内存限制较小 解决方法一: 直接将弹出框中的红色部分修改为需要的内存限制,并选择 Sh ...

  6. Nginx的location配置规则梳理

    Nginx几乎是当下绝大多数公司在用的web应用服务,熟悉Nginx的配置,对于我们日常的运维工作是至关重要的,下面就Nginx的location配置进行梳理: 1)location匹配的是nginx ...

  7. Redis常用操作-------Set(集合)

    1.SADD key member [member ...] 将一个或多个 member 元素加入到集合 key 当中,已经存在于集合的 member 元素将被忽略. 假如 key 不存在,则创建一个 ...

  8. alpa开发阶段团队贡献分

    这是我们团队之前决定的分配方式: 1.凡是认真完成自己任务的队员,都将有基础分30分(态度分). 2. 将整个项目细化为不同的任务,列出一个任务清单,在综合.协调完每名成员的意愿后,我会分配清单中的任 ...

  9. 个人博客作业Week 3 ——微软必应词典客户端

    产品:必应词典客户端 (http://bing.msn.cn/dict/)必应词典有PC,Win8/10, Windows Phone,iPhone,Android,iPad 客户端 选择客户端为:i ...

  10. linux学习笔记【转】

    学习计时:共24小时 读书:1小时 代码:8小时 作业:3小时 博客:12小时 一.学习目标 1. 能够独立安装Linux操作系统   2. 能够熟练使用Linux系统的基本命令   3. 熟练使用L ...