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 = ;i < n; i++) cin >> key,s.insert(key); ll ans = ;
while(s.size() > ){
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 ;
}
// writen by zhangjiuding

POJ 3253 Fence Repair C++ STL multiset 可解 (同51nod 1117 聪明的木匠)的更多相关文章

  1. poj 3253 Fence Repair (STL优先队列)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...

  2. POJ 3253 Fence Repair(修篱笆)

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

  3. POJ 3253 Fence Repair (优先队列)

    POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...

  4. poj 3253 Fence Repair 优先队列

    poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...

  5. POJ 3253 Fence Repair (贪心)

    Fence Repair Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit ...

  6. POJ 3253 Fence Repair 贪心 优先级队列

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 77001   Accepted: 25185 De ...

  7. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

  8. poj 3253 Fence Repair 贪心 最小堆 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=3253 题解 本题是<挑战程序设计>一书的例题 根据树中描述 所有切割的代价 可以形成一颗二叉树 而最后的代价总和是与子节点和深 ...

  9. poj 3253 Fence Repair

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 42979   Accepted: 13999 De ...

随机推荐

  1. Ubuntu16.04下沙盒数据导入到 Neo4j 数据库(图文详解)

    不多说,直接上干货! 参考博客 http://blog.csdn.net/u012318074/article/details/72793914   (表示感谢)  前期博客 Neo4j沙盒实验申请过 ...

  2. Java之NoSuchMethodError

    Java之NoSuchMethodError 最近生产环境出现的一个问题,NoSuchMethodError,之前遇到过,大概明白就是方法冲突.这里总结一下,以备学习之用. 错误代码如下: 2018- ...

  3. Excel导入到DataTable ,DataTable导入到Excel

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS. ...

  4. VB入门在线视频教程大全学习

    课程目录 9分钟47秒 课时1第一课:怎么编写程序 14分钟1秒 课时1第十七课第1节:文件读写的几种方式 14分钟14秒 课时2第二课:什么是变量和变量类型 19分钟24秒 课时3第三课:变量的声明 ...

  5. intell-

    intellect: n.[U, C] the ability to think in a logical way and understand things, especially at an ad ...

  6. Java之秒杀活动解决方案

    0 引言 本文主要描述,服务端做相关秒杀活动的时候,对应的解决方案,即高并发下的数据安全. 1 优化方案 1.1 乐观锁思路 Redis中的watch,请求时,通过Redis查询当前抢购数据,如果当前 ...

  7. FaceBook SDK登录功能实现(Eclipse)

    由于公司游戏要进行海外推广,所以要我们接入FBSDK 实现登录,分享,投放,所以写这篇文章,也算是个工作总结.1.资料 (1).FB SDK github源码地址为 (2): [FB SDK中文接入文 ...

  8. 池(Pool)

    #1 就是一个资源的集合,用的时候按照你的需要去取,用完了给人家放回去 #2 学编程的时候,老师给我们的解释过池的意思,大概是: 如果你喝水,你可以拿杯子去水龙头接.如果很多人喝水,那就只能排队去接. ...

  9. PHP的soap 之 nusoap 的使用

    今天不知道为什么想起了soap 这个东西,然后就弄了下,在php上使用的是nusoap. 一些基本的使用,高深的麻烦您自己看手册去 这个软件的下载在http://sourceforge.net/pro ...

  10. Linux下pyftplib简单的脚本

    from pyftpdlib.authorizers import DummyAuthorizer from pyftpdlib.handlers import FTPHandler from pyf ...