题目链接:点击打开链接

Fence Repair

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 59979   Accepted: 19802

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

思路:优先级队列,取两个,把和再放进去。

AC代码:

#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
using namespace std;int n, x;
int main() {while(~scanf("%d", &n)) {priority_queue<int, vector<int>, greater<int> > pq;for(int i = 0; i < n; i++) {scanf("%d", &x);pq.push(x);}long long sum = 0;while(pq.size() > 1) {int a = pq.top();pq.pop();int b = pq.top();pq.pop();pq.push(a+b);sum += (a+b);}printf("%lld\n", sum);}
}

转载于:https://www.cnblogs.com/ACMerszl/p/9572964.html

POJ3253-Fence Repair相关推荐

  1. POJ3253 Fence Repair【哈夫曼树+优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 48026   Accepted: 15784 De ...

  2. poj3253 Fence Repair(贪心+哈夫曼 经典)

    https://vjudge.net/problem/POJ-3253 很经典的题,运用哈夫曼思想,想想很有道理!! 具体实现还是有点绕人,最后被long long卡了一下,看数据大小的时候单纯相乘了 ...

  3. [POJ3253]Fence Repair

    题目链接:http://poj.org/problem?id=3253 单调队列和优先队列(哈夫曼编码)分别实现: 1 #include <algorithm> 2 #include &l ...

  4. [BZOJ1724][Usaco2006 Nov]Fence Repair 切割木板

    1724: [Usaco2006 Nov]Fence Repair 切割木板 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 1272  Solved: ...

  5. 老BOJ 07 Fence Repair

    Fence Repair Accept:199     Submit:784 Time Limit:1000MS     Memory Limit:65536KB Description Farmer ...

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

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

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

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

  8. POJ 3253 -- Fence Repair

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

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

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

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

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

最新文章

  1. SECTIONS bss段初始化分析
  2. libtorch error C1021: 无效的预处理器命令“warning”
  3. TENSORFLOW GUIDE: EXPONENTIAL MOVING AVERAGE FOR IMPROVED CLASSIFICATION
  4. Python3实现打家劫舍问题
  5. Python编程基础16:类和对象
  6. c语言10个裁判,5个裁判可以对10个歌手进行打分,计算各个歌手的最终得分排列...
  7. 菜刀php提权,靶机SickOs1.2练习:上传马文件菜刀连接/反弹shell,进行本地提权
  8. 阿里否认马云辞职;刘强东继续管京东;滴滴开始全程录音 | 极客头条
  9. linux ext3下删除mysql数据库的数据恢复案例
  10. Pytorch中的train和eval模式详解
  11. 空间apiLinux系统调用及用户编程接口(API)学习
  12. 二分法查找——C++代码
  13. 使用ssh远程重装centos
  14. 从零开始的iOS开发:10 | 如何构建一个APP
  15. android调试遇到ADB server didn't ACK以及蛋疼的sjk_daemon进程
  16. 如何获取微信开发者id及设置微信授权目录
  17. windows快捷键,选中鼠标所指以上或以下内容
  18. LEFT JOIN 与 RIGHT JOIN
  19. Git的原理详解与使用-臧雪园-专题视频课程
  20. c语言监视,C语言设置监视哨的顺序查找、折半查找算法的实现

热门文章

  1. Atitit web 之道 艾龙著 Atitit web 之道 艾龙艾提拉著v2 saa.docx 1. 第1章 Web编程基础知识 (1) 3 1.1. 1.1 什么是Web (1) 3 1.2.
  2. Atitit 演讲常用肢体语言与手势总结 目录 1. 原则 ,哑语一样,手势不只是补充。。。 1 2. 比拟实际物体与抽象物体 1 2.1. 三个实用的手势: 1 2.2. (五)、演讲中忌讳的动作
  3. Atiti 数据库系统原理 与数据库方面的书籍 attilax总结 v3 .docx
  4. Atiti. Php Laravel 5.1 环境搭建以及  error 排除
  5. paip.修复文件关联总结
  6. paip.图形化编程工具VS WF与JBPM的比较
  7. paip.php debug 调试---与OPTIMIZER共存
  8. Rust :CC编译
  9. (转)黑石帝国崛起的30年
  10. (转)别只盯着比特币!“野蛮生长”的ICO江湖:2年30倍只是寻常