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

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

 
之前我们用数组实现了这个贪心,简单且很容易就AC了
但是,还有更简单,并且更快的写法
那就是优先级队列,直接调用STL实现即可
 
 #include <cstdio>
#include <iostream>
#include <queue> using namespace std; const int max_n=;
const int max_L=; typedef long long LL; int n;
int L[max_n]; void solve()
{
LL ans=;
// 建立最小优先队列
priority_queue< int,vector<int>,greater<int> > que;
for(int i=;i<n;++i)
{
que.push(L[i]);
} int tmp;
while(que.size()>=)
{
tmp=que.top();
que.pop();
tmp+=que.top();
que.pop(); ans+=tmp;
que.push(tmp);
} printf("%lld",ans);
} int main()
{
scanf("%d",&n);
for(int i=;i<n;++i)
{
scanf("%d",&L[i]);
}
solve();
return ;
}

嗯,代码和行数只有之前的一半,不经思考直接调库还不损耗脑细胞。

关键是,更快了
 
21294701 LIUYUANHAO 3253 Accepted 220K 844MS C++ 1741B

2020-02-02 18:11:44

21310136 LIUYUANHAO 3253 Accepted 344K 0MS C++ 665B

2020-02-05 17:31:23

 
复杂度从o(n^2) 降到了 o( n*log(n) )

POJ 3253 Fence Repair 贪心 优先级队列的更多相关文章

  1. POJ 3253 Fence Repair (贪心)

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

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

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

  3. POJ 3253 Fence Repair 贪心+优先队列

    题意:农夫要将板割成n块,长度分别为L1,L2,...Ln.每次切断木板的花费为这块板的长度,问最小花费.21 分为 5 8 8三部分.   思路:思考将n部分进行n-1次两两合成最终合成L长度和题目 ...

  4. POJ 3253 Fence Repair(修篱笆)

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

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

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

  6. poj 3253 Fence Repair 优先队列

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

  7. POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 53645   Accepted: 17670 De ...

  8. POJ - 3253 Fence Repair 优先队列+贪心

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

  9. poj 3253 Fence Repair(priority_queue)

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

随机推荐

  1. springBoot 整合 dubbo 遇到的坑

    一.注意springBoot 和 dubbo 之间版本的问题 <?xml version="1.0" encoding="UTF-8"?> < ...

  2. ubuntu18.04编译jdk8

    准备编译环境 sudo apt-get install -y zip unzip build-essential libx11-dev libxext-dev libxrender-dev libxt ...

  3. idea 2019.3 破解激活码

    idea激活码(亲测 idea 2019.3可用)有效期到2021年3月: QYYBAC9D3J-eyJsaWNlbnNlSWQiOiJRWVlCQUM5RDNKIiwibGljZW5zZWVOYW1 ...

  4. 关于2D渲染的一些小想法

    原文地址 概述 . 这个项目最初的目的是为了尝试解析现有的UI编辑器(MyGUI)导出的UI布局信息,通过ImGUI还原UI渲染.但是在开发过程中,我发现可以借此实现一个编辑器,一个我不断的寻找,但始 ...

  5. .net core 认证与授权(二)

    前言 这篇紧接着一来写的,在第一篇中介绍了认证与授权,同时提出了这套机制其实就是模拟现实中的认证与授权. 同样这篇介绍在这套机制下,用户信息管理机制?这里就会问了,上一篇中认证和授权不是都ok了吗,怎 ...

  6. [apue] 书中关于伪终端的一个纰漏

    在看 apue 第 19 章伪终端第 6 节使用 pty 程序时,发现“检查长时间运行程序的输出”这一部分内容的实际运行结果,与书上所说有出入. 于是展开一番研究,最终发现是书上讲的有问题,现在摘出来 ...

  7. FPGA基础知识关键点摘要

    FPGA基础知识关键点摘要 一.组合逻辑和时序逻辑的区别:组合逻辑与输入直接实时相关,时序逻辑还必须在时钟上升沿出发后输出新值,有没有时钟输入是他们最大的区别!组合时序容易出现竞争冒险现象出现亚稳态, ...

  8. HDU 6602 Longest Subarray (线段树)

    题意: 1e5的数组,c(1e5)种数字求最长的子串,使得其中每个出现的数字出现的次数为0次或者大于k次 思路: 枚举右端点i,维护当前右端点时,每个左端点的可行元素数量,当且仅当可行元素为c时更新答 ...

  9. 初学maven的一些配置

    初学Maven的一些配置 1.maven的安装 2.从官网下载3.6.1版本后,高级版本可能会出现不兼容 jdk1.8 3.配置maven 在 settings.xml <settings> ...

  10. num11---桥接模式

    比如手机类,有各种类型,比如翻盖.平板等,每一类下又有各个品牌,比如华为,如果新增一个类型,比如折叠屏,或者新增一个手机品牌,苹果,那么会导致 扩展性问题. 这种情况下,应该使用桥接模式. 代码: 创 ...