Fence Repair
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 53106 Accepted: 17508
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).

思路:在集合中每次找最短的两个加起来,算到ans里。
   再将这两个数从集合中删除,把他们的和加入到集合里。

  用multiset解的话,插入和查找都是O(log(n)),再加上遍历一遍,总复杂度是:O(nlog(n))。

代码:

#include <iostream>
#include <set>
using namespace std;
typedef long long ll;multiset <int> s;int main() {int n,key;cin >> n;for(int i = 0;i < n; i++) cin >> key,s.insert(key);ll ans = 0;while(s.size() > 1){int k1 = *s.begin();s.erase(s.begin());int k2 = *s.begin();s.erase(s.begin());int t = k1+k2;ans += t;s.insert(t);}cout << ans << endl;return 0;
}
//  writen by zhangjiuding

POJ 3253 Fence Repair C++ STL multiset 可解相关推荐

  1. POJ 3253 Fence Repair(修篱笆)

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

  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、Saruman's Army

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

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

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

最新文章

  1. python爬百度翻译-爬虫 python爬取百度翻译接口 超详细附源码
  2. linux中的c 环境变量,Linux C 参考手册 之 环境变量篇
  3. 移动设备真机调试本地程序的Node.js【无需连wifi】
  4. extern相关问题
  5. js 的 math 函数
  6. MFC中实现模态对话框的结构与原理
  7. es算法matlab编程,matlab练习程序(演化策略ES)
  8. 网络虚拟化有几种实现方式_停车场管理系统的防砸车功能有几种方式?如何实现?...
  9. 表单oninput和onchange事件区别
  10. Microsoft.Web.RedisSessionStateProvider 运行异常问题
  11. java timer 序列化_Java中的定时器Timer使用示例代码
  12. 官方client php api接口日记
  13. git flow命令
  14. atitit.团队建设--要不要招技术储备人才的问题
  15. WP7 手机软件纪念 - 稍后读软件
  16. Excel构建决策分析模型
  17. 乐视网正式聘用刘延峰担任公司总经理 任期三年
  18. Spring Boot 项目 启动 端口经常被占用 彻底解决方案
  19. 襄阳职业技术学院计算机技能高考,2021湖北技能高考襄阳职业技术学院怎么报名?...
  20. 【构成L1笔记:飞行道具】

热门文章

  1. stm8s103k3 周期 捕获_STM8S103K3 - 主流基本型系列8位MCU,具有8 KB Flash、16 MHz CPU和集成EEPROM - STMicroelectronics...
  2. 用户画像标签维度_神策数据钟秉哲:一文了解用户标签画像,从洞察到突破
  3. 解决方案和项目的区别_沃尔玛用大数据提高销售额,云计算和大数据技术之间的区别汇总...
  4. python打开文件夹的权限_python修改linux中文件(文件夹)的权限属性操作
  5. 计算机函数图象,信息技术应用用计算机绘制函数图象
  6. Debian下Cannot set LC_CTYPE to default locale: No such file or directory解决方法
  7. Jzoj5235 好的排列
  8. Python __dict__和vars()
  9. Eclipse 中切换不同的JDK设置
  10. Bootstrap 学习之js插件(模态框(Modal)插件)