Fence Repair(poj3253)
题目链接: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
Lines 2..N+1: Each line contains a single integer describing the length of a needed plank
Output
Sample Input
3
8
5
8
Sample Output
34
Hint
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)的更多相关文章
- POJ 3253 Fence Repair (优先队列)
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair (贪心)
题意:将一块木板切成N块,长度分别为:a1,a2,……an,每次切割木板的开销为当前木板的长度.求出按照要求将木板切割完毕后的最小开销. 思路:比较奇特的贪心 每次切割都会将当前木板一分为二,可以按切 ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- Greedy:Fence Repair(POJ 3252)
Fence Repair 问题大意:农夫约翰为了修理栅栏,要将一块很长的木块切割成N块,准备切成的木板的长度为L1,L2...LN,未切割前的木板的长度恰好为切割后木板的长度的总和,每次切断木板的时候 ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- POJ 3253 Fence Repair(哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26167 Accepted: 8459 Des ...
- Fence Repair (二叉树求解)(优先队列,先取出小的)
题目链接:http://poj.org/problem?id=3253 Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Sub ...
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
随机推荐
- flask-socketio笔记
Flask-SocketIO使Flask应用程序可以访问客户端和服务器之间的低延迟双向通信. 客户端应用程序可以使用Javascript,C ++,Java和Swift中的任何SocketIO官方客户 ...
- ASP.NET MVC5+EF6+EasyUI 后台管理系统(63)-WebApi与Unity注入
系列目录 前言: 有时候我们系统需要开放数据给手机App端或其他移动设备,不得不说Asp.net WebApi是目前首选 本节记录Asp.net MVC WebApi怎么利用Unity注入.系列开头已 ...
- inode 软/硬链接
一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...
- 挂载银行前置机Ukey到windows server2012虚拟机的操作记录
公司有跟银行对接的金融业务,需要配置银行前置机环境.通过KVM的WebVirtMgr管理平台创建windows server2008虚拟机,安装参考:kvm虚拟化管理平台WebVirtMgr部署-完整 ...
- 太白教你学python---博客分类目录
太白非技术类随笔(持续更新中...猛击这里!!!) python基础 python基础一 pytcharm安装详细教程 python基础二 python基础数据类型 Python最详细,最深入的代码块 ...
- Python_命名空间和作用域_25
# 函数进阶 a = def func(): print(a) func() # 命名空间和作用域 # print() # input() # list # #命名空间 有三种 #内置命名空间 —— ...
- vue自定义公共组件components||在vue中,解决修改后的数据不能渲染到dom上的bug
//主页面框架用来嵌入:Main.vue <el-col :span="24" > * { margin: 0; padding: 0; } html { width: ...
- linux内实践核分析模块
- Geekers团队成立日志
大家好,作为团队的队长,今天在这里非常荣幸能够发表我们团队的第一篇博客,来宣布我们团队的名字:Geekers! Geek,英文中代表“怪人”,随着时代进步Geek被赋予了新的含义——极客!Steve ...
- GitHub18
兴趣是最好的老师,HelloGitHub 就是帮你找到兴趣! 简介 分享 GitHub 上有趣.入门级的开源项目. 这是一个面向编程新手.热爱编程.对开源社区感兴趣 人群的月刊,月刊的内容包括:各种编 ...