POJ 3253 Fence Repair(修篱笆)

Time Limit: 2000MS   Memory Limit: 65536K

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

农夫约翰打算修理下围绕牧场的一小段篱笆。他量了量篱笆发觉需要N(1 ≤ N ≤ 20,000)块木板,每块木板分别需要Li(1 ≤ Li ≤ 50,000)整的单位长度。然后他买了块长到可以切出这N块木板的长木板(换句话说,它的长度是Li的总和)。FJ会忽略锯开木板时变成木屑的“锯痕”;所以你也应该忽略它。

FJ悲伤地发现自己没有锯子用来据木头,因此他带着长木板去农夫Don的农场借去了。

怎料农夫Don是个抠门的资本家,并不想借给FJ,但可以有偿帮他切N-1次木板。切木板的费用与木板长度相等。却一块长21的木板需要花21美分。

农夫Don让农夫约翰自己决定怎么切。请帮农夫约翰确定完成这N块板后的最小费用。FJ知道使用不同切割方式会造成不同的花费,因为中途的木板长度不同。

【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

第1行:一个整数N,表示木板的数量。

第2~N+1行:每行一个整数,表示需要木板的长度。

【Output】

【输出】

Line 1: One integer: the minimum amount of money he must spend to make N-1 cuts

第一行:约翰进行N-1次切割的最小花费

【Sample Input - 输入样例】

【Sample Output - 输出样例】

3

8

5

8

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

约翰想把长度为21的长木板切成8,5,和8.

初始木板的长度为8+5+8=21。初次切割花费21,然后应被切成13与8。第二次切割花费13,然后应被切成8和5。此时的花费是21+13=34。如果21被改切成16和5,第二次切割花费16后总费用为37(这样比34来得大)。

【题解】

  贪心plus,用哈夫曼树的合并思想。

  一开始想先把最短的板分出去,不过算了算结果好像不对。

  应该把一块长板分成尽可能长的两块短板,之后逆向实现就好了。

  需要这里要注意的是数据会超int

【代码 C++】

 1 #include<cstdio>
 2 #include<queue>
 3 #include<functional>
 4 std::priority_queue<int, std::vector<__int64>, std::greater<int> > data;
 5 int main(){
 6     int n, i;
 7     __int64 temp, opt;
 8     scanf("%d", &n);
 9     for (i = opt = 0; i < n; ++i){
10         scanf("%I64d", &temp);
11         data.push(temp);
12     }
13     if (n == 1) printf("%I64d", data.top());
14     else{
15         for (i = 1; i < n; ++i){
16             temp = data.top(); data.pop();
17             temp += data.top(); data.pop();
18             opt += temp; data.push(temp);
19         }
20         printf("%I64d", opt);
21     }
22     return 0;
23 }

转载于:https://www.cnblogs.com/Simon-X/p/5322395.html

POJ 3253 Fence Repair(修篱笆)相关推荐

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

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

  2. POJ 3253 -- Fence Repair

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

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

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

  4. POJ 3253 Fence Repair 贪心

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

  5. POJ No. 3253 Fence Repair

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

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

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

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

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

  8. 编程算法 - 篱笆修理(Fence Repair) 代码(C)

    篱笆修理(Fence Repair) 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 把一块木板切成N块, 每次切两块, 分割的开销是木板长度, ...

  9. 农夫约翰修篱笆-堆排序

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

最新文章

  1. C语言 string.h 中函数的实现
  2. 网站新手引导/步骤引导
  3. linux下top命令讲解
  4. mysql在线修改表结构大数据表的风险与解决办法归纳
  5. 简单的错觉画_错觉图片生成实验 - 正方形错觉
  6. .NET 6 Talk Party 2|.NET Core 与行业
  7. MySQl Got a packet bigger than ' max_allowed_packet' bytes
  8. hash的算法 java_【数据结构与算法】一致性Hash算法及Java实践
  9. C语言系列文章之#和##
  10. 加拿大生信开源学习资源Bioinformatics.ca
  11. 不得不看的cookie和session
  12. 关于“想哭”病毒,我也来两句。--转载
  13. scp连接linux网络错误,winscp连接linux(centos7)时提示主机超过15秒无通信,继续等待的解决方法...
  14. Linux之远程连接服务器ssh、telnet
  15. 比较sql server两个数据库
  16. 技术分解:光纤传感在交通监控中的应用
  17. 大数据之Kafka介绍
  18. Java学习之常用的Java构建工具
  19. 懒人数据库 MongoDB 5.x
  20. google earth engine随缘学习(十一)影像分割

热门文章

  1. idea中用java不能自动导包的解决办法
  2. 新手上路必备的 DAX 函数(下)
  3. hive的UDF函数的使用。常见UDF函数
  4. 基于FPGA的简易DDS信号发生器的设计(一)
  5. 数据库 PolarDB 开源之路该如何走?听听他们怎么说
  6. 如何在万网购买一个属于自己的域名
  7. 扒开ARM中断控制器的底裤来看看!
  8. java定义语法解析器,java开发工具intellij idea使用教程:定义语法和解析器.pdf
  9. USB总线驱动及鼠标驱动实例
  10. 通信原理day7:第三章:抽样;均匀量化;非均匀量化;A律;增量(ΔM)调制