优先队列 POJ 3253 Fence Repair
题意:一块木板按照某个顺序切成a[1], a[2]...a[n]的长度,每次切都会加上该两段木板的长度,问选择什么顺序切能使得累加和最小
分析:网上说这是哈夫曼树。很容易想到先切掉最长的,反过来也就是相当于每次取最短的两块合并成一块,直到最后剩下原来的一块,优先队列实现

代码:
/************************************************
* Author :Running_Time
* Created Time :2015/9/14 星期一 08:51:15
* File Name :POJ_3253.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 2e4 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int a[N]; int main(void) {
int n;
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
priority_queue<int, vector<int>, greater<int> > Q;
for (int i=1; i<=n; ++i) Q.push (a[i]);
ll ans = 0;
while (Q.size () > 1) {
int l1 = Q.top (); Q.pop ();
int l2 = Q.top (); Q.pop ();
ans += l1 + l2;
Q.push (l1 + l2);
}
printf ("%I64d\n", ans);
} return 0;
}
优先队列 POJ 3253 Fence Repair的更多相关文章
- POJ 3253 Fence Repair (优先队列)
		
POJ 3253 Fence Repair (优先队列) Farmer John wants to repair a small length of the fence around the past ...
 - poj 3253 Fence Repair 优先队列
		
poj 3253 Fence Repair 优先队列 Description Farmer John wants to repair a small length of the fence aroun ...
 - POJ 3253 Fence Repair(修篱笆)
		
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
 - poj 3253 Fence Repair(优先队列+哈夫曼树)
		
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
 - POJ - 3253 Fence Repair 优先队列+贪心
		
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
 - [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
		
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
 - POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
		
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
 - POJ 3253 Fence Repair (贪心)
		
Fence Repair Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
 - POJ 3253 Fence Repair 贪心 优先级队列
		
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 77001 Accepted: 25185 De ...
 
随机推荐
- 在VC中动态加载ODBC的方法
			
在使用VC.VB.Delphi等高级语言编写数据库应用程序时,往往需要用户自己在控制面板中配置ODBC数据源.对于一般用户而言,配置ODBC数据源可能是一件比较困难的工作.而且,在实际应用中,用户往往 ...
 - Jetty的JNDI数据源
			
一. 此处绑定的数据源是以 DBCP 为实现.首先必须将数据库驱动(这里用了MYSQL数据库)和DBCP所需要的 Jar 包复制到 Jetty 根目录的 lib 目录下.DBCP主要需要以下3个文件: ...
 - 20170218-取值Domain
			
1.通过视图DD07V, 例子:取SD 凭证类别的DOMAIN(VBTYP)DATA: LT_DD07V TYPE TABLE OF DD07V.SELECT * INTO T_DD07V FROM ...
 - C#  partial分部类和分部方法
			
1.https://www.cnblogs.com/xcsn/p/7533238.html 它是一个关键字修饰符.可以将类或结构.接口或方法的定义拆分到两个或更多个源文件中. 每个源文件包含类型或方法 ...
 - DataSnap Mobile Client Tutorial
			
One of my customers was having some difficulty following the DataSnap tutorial which can be found he ...
 - 网站页面打开浏览器table中显示图片
			
就类似博客园这种:
 - phpMVC框架的核心启动类定义
			
<?php//核心启动类class Framework { //定义一个run方法 public static function run(){ // echo "hello,wrold ...
 - 创建Android本地repo
			
/**************************************************************************** * 创建Android本地repo * 说明 ...
 - I.MX6 android 源码下载
			
/************************************************************************* * I.MX6 android 源码下载 * 说明 ...
 - [Selenium] The commonly used validation method
			
Assert.assertTrue(tmpEl.getAttribute("class").contains("selected"),"The fol ...