poj 3253 Fence Repair
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 42979 | Accepted: 13999 |
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).
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int l[];
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(l,,sizeof(n));
for(int i=;i<n;i++)
{
scanf("%d",l+i);
}
long long ans=;
while(n>)
{
int sm1=,sm2=;
if(l[sm1]>l[sm2]) swap(sm1,sm2);
for(int i=;i<n;i++)
{
if(l[i]<l[sm1])
{
sm2=sm1;
sm1=i;
}
else if(l[i]<l[sm2])
{
sm2=i;
}
}
long long t=l[sm1]+l[sm2];
ans+=t; if(sm1==n-) swap(sm1,sm2);
l[sm1]=t;
l[sm2]=l[n-];
n--; }
printf("%lld\n",ans);
}
return ;
}
没割一次,都会分成两段,所以可以看作一个求最小二叉树的题。贪心。据说还可以lg级的算法算出。
以后再说
poj 3253 Fence Repair的更多相关文章
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- 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 优先队列
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
- POJ 3253 Fence Repair (贪心)
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3253 Fence Repair 贪心 优先级队列
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
- poj 3253:Fence Repair(堆排序应用)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 23913 Accepted: 7595 Des ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair (哈夫曼树)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19660 Accepted: 6236 Des ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
随机推荐
- U盘因为装linux系统变小了
U盘在Windows下被UltraISO等软件制作成Linux启动盘后会自动被格式化成FAT格式,导致容量变小. 用DiskGenius去修复 http://www.jb51.net/softs/75 ...
- linux 下向github上传代码
上传代码: cd TPS/devices/M8 git init #//初始化 git add . #如果是.表示上传全 ...
- iOS7时代我们用什么来追踪和识别用户?
要识别用户,首先就是要选择一个标识符,通过这个标识符来识别这个用户的设备(而不是用户),这个标识符要能够保证一个设备上返回的值是一样的,并且在其他设备上不会出现相同的值. 在iOS7之前,曾经有过很多 ...
- 5 构建Mysql+heartbeat+DRBD+LVS集群应用系统系列之生产环境下drbd裂脑处理
preface 公司的业务变更,导致服务器要搬迁,所以需要关闭服务器,然后到新地在开启服务器. 关机前确定drbd+heartbeat+mysql是正常使用的,没有异常,Heartbeat和drbd都 ...
- HTML3
1. HTML5新增的标签 主体结构标签, 用来做布局.比div更具语义 <header> 定义文档的页眉 <nav> 定义导航链接的部分 <article> 规定 ...
- JavaScript 数组中的 indexOf 方法
let arr = ['orange', '2016', '2016']; arr.indexOf('orange'); //0 arr.indexOf('o'); //-1 arr.indexOf( ...
- java中的wait(),notify(),notifyAll(),synchronized方法
wait(),notify(),notifyAll()三个方法不是Thread的方法,而是Object的方法.意味着所有对象都有这三个方法,因为每个对象都有锁,所以自然也都有操作锁的方法了.这三个方法 ...
- rcnn学习(六):imdb.py学习
# -------------------------------------------------------- # Fast R-CNN # Copyright (c) 2015 Microso ...
- 一个简单的servlet的demo
javaweb 的应用我们需要参考javaee api 查找servlet接口 javax.servletInterface Servlet All Known Subinterfaces: Ht ...
- 找出只含有2,3,5因子构成的数的第N个
https://leetcode.com/problems/ugly-number-ii/ 刚开始,一看题以为用刷选法,但是当数据量大时明显不行.然后感觉用含2,3,5因子的个数当做进制来处理还是不行 ...