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. docker挂载war包到tomcat容器中的注意点和坑

    刚开始用docker,难免会遇到很多坑,这里分享一下: 一 挂载最好挂载目录 我刚开始挂载war包,结果发现容器里把挂载的war包当成目录了 二 本地路径必须是绝对路径,否则不管用 三 容器中使用vi ...

  2. orcle导入大数据文件

    0,创建ctl文件  内容为 OPTIONS (skip=0) LOAD DATA  CHARACTERSET 'UTF8' INFILE 'F:\anhui_data\20180814\shangh ...

  3. Java支付宝PC网站支付功能开发(详细教程)

    一.前言 本案例使用的是Java实现的.使用支付宝的沙盒环境示例.发布需要换成正式环境.这里就不作详细说明了 本代码适合用来做参考,不要直接复制去使用. 没有账号的需要去平台注册一个: 登录支付宝开发 ...

  4. 实验10: RIP

    实验7-1 :  RIPV1 实验目的通过本实验可以掌握:1. 在路由器上启动RIPv1 路由进程2. 启用参与路由协议的接口,并且通告网络3. 理解路由表的含义4. 查看和调试RIPv1 路由协议相 ...

  5. 超实用的Eclipse快捷键大全(解密为什么他们的代码写的又快又好~)

    1.Ctrl+s:快速保存代码 一定要记得随时随地用Ctrl+s来保存我们的代码哦!!!不然等到电脑关机或者是使用的Eclipse突然闪退就欲哭无泪了.此时脑海里就突然出现了哔哔哔的画面~ 2.Alt ...

  6. qt客户端程序使用svg图片资源的几种方法

    直接使用svg格式文件资源的情况 1. 直接在UI控件属性面板中选择部分支持icon图标的控件的icon来源,这样图标可以显示 2.给toolbutton添加样式 qproperty-icon: ur ...

  7. JAVA环境配置(Windows10)

    Java开发环境配置 下载JDK 首先我们需要下载java开发工具包JDK,下载地址:https://www.oracle.com/technetwork/java/javase/downloads/ ...

  8. HDU_1556_线段树

    http://acm.hdu.edu.cn/showproblem.php?pid=1556 直接用了技巧来做. #include<iostream> #include<cstdio ...

  9. POJ_1208_模拟

    题目描述: 给定一个长度n,有0~n-1编号的箱子和位置,起始个编号的箱子放在相同编号的位置. 有一系列操作: move a onto b,将a,b上面的箱子放回初始位置,并将a放到b箱上. move ...

  10. Go语言实现:【剑指offer】平衡二叉树

    该题目来源于牛客网<剑指offer>专题. 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...