题干:

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).

Source

USACO 2006 November Gold

题目大意:

给一块长木板,现要将其锯成n段,共需锯n-1次,每次锯的代价为所锯木板的长度,求最小总代价。

解题报告:

若把木板切割过程画成一个树的话,根就是总长度,枝叶是n段切割后的木板长度,显然费用就是所有的节点的和,所以也就是 叶节点的大小*节点深度,所以我们希望深度越大的木板越短越好。所以逆向生成一棵树,从叶节点开始一次合并最小和次小木板直到生成一棵树。或用交换排序贪心法去思考。

AC代码:

#include<cstdio>
#include<queue>
#include<vector>
using namespace std;
priority_queue<int,vector<int>,greater<int> >que;//小根堆
int n;
long long ans;
int main(){scanf("%d",&n);for(int i=1,x;i<=n;i++){scanf("%d",&x);que.push(x);}for(int i=1,x,y;i<n;i++){x=que.top();que.pop();y=que.top();que.pop();ans+=x+y;que.push(x+y);}printf("%lld\n",ans);return 0;
}

注意一个错误代码:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#define ll long long
using namespace std;
ll a[200000];int main()
{int n;cin>>n;ll sum = 0,ans = 0;for(int i = 1; i<=n; i++) {scanf("%lld",&a[i]);sum += a[i];}sort(a+1,a+n+1);if(n == 1){printf("0\n");return 0;}ans = a[1] + a[2];for(int i = 3; i<=n; i++) {ans += ans + a[i];}printf("%lld\n",ans);return 0 ;} 

错误原因:直接把刚求出来的ans接着带入求了,这样是不对的!因为这个ans不一定被接着用。。

总结:

有坑:注意这题说5e4 * 2e4的数据量貌似刚刚好1e9,貌似可以int,但是这题必须longlong才能过!原因自己想。(其实ans只要开longlong就可以了)

【POJ - 3253】Fence Repair(贪心,时光倒流)相关推荐

  1. POJ 3253 Fence Repair 贪心

    每次合并选最短的两块木板,用优先队列优化. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include&l ...

  2. POJ 3253 Fence Repair(修篱笆)

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

  3. POJ 3253 -- Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 55661   Accepted: 18331 De ...

  4. POJ 3253 Fence Repair C++ STL multiset 可解

    Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53106 Accepted: 17508 Descri ...

  5. 贪心算法-----poj 3253 Fence Repair(切木板)

    Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...

  6. POJ No. 3253 Fence Repair

    POJ No. 3253 Fence Repair 官方地址 题目 农夫约翰为了修理栅栏,要将一块很长的木板切割成N块.准备切成的木板的长度为L1.L2.-.LN,未切割前木板的长度恰好为切割后木板长 ...

  7. POJ 3253 - Fence Repai ( 优先队列 )

    题意 切割木板, 比如一根长21的木板要切割成5, 8, 8的三块, 每次切割花费的金额为两断的长度. 比如先把21切成16和5, 花费21元, 再把16切成8和8, 花费16元, 总计消费37元. ...

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

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

  9. 1724: [Usaco2006 Nov]Fence Repair 切割木板( 贪心 )

    倒过来看 , 每次总是选择最短的两块木板合并 , 用heap维护 ------------------------------------------------------------------- ...

  10. 贪心:Fence Repair、Saruman's Army

    Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...

最新文章

  1. NLP进阶之(七)膨胀卷积神经网络
  2. T-SQL 编码标准
  3. window powershell 替换文本的字符
  4. 尼康d850相机参数测试软件,尼康 D850最全参数信息曝光 快来围观
  5. react前端显示图片_在react里怎么引用图片
  6. linux ubuntu deepin apache2 rewrite
  7. linux下web压力测试工具ab使用及详解
  8. 如果有一天,小夕不再萌...
  9. Web缓存相关知识整理
  10. MongoDB中文社区年终盛典
  11. Android端公司通讯录开发与实现(二)
  12. JAVA动态申请数组
  13. 如何使用Excel制作标靶图
  14. 计算机网络传输速度单位换算,网络速度单位换算
  15. 数据结构如何申请一个空间的队列_如何用鞋柜来作为隔断,隔出一个玄关空间...
  16. 树结构——2-3树图解
  17. i了i了,这简直就是2020全网最全最强的MyBatis面试题!
  18. 打造前端 Deepin Linux 工作环境——安装最新版本的火狐firefox浏览器
  19. VS Code 下载和安装教程
  20. 系统学习机器学习之随机场(三)--MRF,CRF及几种模型对比

热门文章

  1. AC自动机:多模式串匹配实现敏感词过滤
  2. java 内部thread_Java代码质量改进之:使用ThreadLocal维护线程内部变量
  3. tableau地图城市数据_举个栗子!Tableau 技巧(156):在地图分析中创建缓冲区
  4. c语言编程流水灯与交通灯实验,C51单片机实验报告_流水灯_交通灯_定时器_双机交互_时钟.doc...
  5. zookeeper 密码_阿里资深JAVA架构带你深度剖析dubbo和zookeeper关系
  6. php搭建的网站空白,使用phpstudy搭建dedecms网站后台页面空白解决方法
  7. Ubuntu 14.04 为 root 帐号开启 SSH 登录
  8. ubuntu默认root密码
  9. 根据rtk参数在arcgis中进行可视化
  10. arcgis字段计算器利用python按不同两列数据进行编号