poj 3253 Fence Repair(模拟huffman树 + 优先队列)
题意:如果要切断一个长度为a的木条需要花费代价a, 问要切出要求的n个木条所需的最小代价。
思路:模拟huffman树,每次选取最小的两个数加入结果,再将这两个数的和加入队列。
注意priority_queue的用法,原型:
priority_queue<Type> q;
priority_queue<Type,deque<Type>,Comp> q;
其中Type是类型,Comp是比较结构体,比较函数是它的括号重载,比如对int型从小到大排序的Comp结构体如下:
struct Comp
{
bool operator()(const LL& a,const LL&b) const
{
return a>b;
}
};
还有要注意的是,不能直接while(!q.empty()),因为每次选取两个数,如果队列只剩下一个数时又要取两个数,此时程序会爆掉,应该用一个while(1)循环,在取出一个数时立刻判断if(q.empty()) break;
不能直接模拟huffman树,直接huffman会超时。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 1000000
#define inf 1e12
ll n;
struct Comp{
bool operator()(const ll &a,const ll &b)const{
return a>b;
}
};
int main()
{
while(scanf("%I64d",&n)==){
priority_queue<ll,deque<ll>,Comp> q;
for(int i=;i<=n;i++){
ll x;
scanf("%I64d",&x);
q.push(x);
}
ll ans=;
while(){
ll L=q.top(); q.pop();
if(q.empty()) break;
ll R=q.top(); q.pop();
ans+=L;
ans+=R;
q.push(L+R);
}
printf("%I64d\n",ans);
}
return ;
}
poj 3253 Fence Repair(模拟huffman树 + 优先队列)的更多相关文章
- [ACM] POJ 3253 Fence Repair (Huffman树思想,优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25274 Accepted: 8131 Des ...
- poj 3253 Fence Repair (贪心,优先队列)
Description Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- 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 (优先队列) Farmer John wants to repair a small length of the fence around the past ...
- POJ 3253 Fence Repair(修篱笆)
POJ 3253 Fence Repair(修篱笆) Time Limit: 2000MS Memory Limit: 65536K [Description] [题目描述] Farmer Joh ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- poj 3253 Fence Repair(优先队列+哈夫曼树)
题目地址:POJ 3253 哈夫曼树的结构就是一个二叉树,每个父节点都是两个子节点的和. 这个题就是能够从子节点向根节点推. 每次选择两个最小的进行合并.将合并后的值继续加进优先队列中.直至还剩下一个 ...
- POJ 3253 Fence Repair【哈弗曼树/贪心/优先队列】
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 53645 Accepted: 17670 De ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
随机推荐
- Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
Entity Framework 技术参考:http://kb.cnblogs.com/zt/ef/
- Rotate Array 解答
Question Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, t ...
- Unique Paths II 解答
Question Follow up for "Unique Paths": Now consider if some obstacles are added to the gri ...
- Container With Most Water 解答
Question Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate ...
- 关于C++中的拷贝构造函数和赋值函数
如果类定义的数据成员中存在指针或引用,那么最好重载这两个函数. 1. 定义 拷贝构造函数的定义格式:构造函数名(const 源类名& 引用对象形参名){} 赋值函数定义格式:源类名 & ...
- mysql 5.7忘记密码处理
vi /etc/my.cnf在[mysqld]下面增加一行skip-grant-tables 重启 /etc/init.d/mysqld restart /usr/local/mysql/bin/m ...
- 在Red Hat Enterprise Linux 5 64-bit安装oracle11g r2
网上这种文档一搜就一堆,没别的目的,刚接触oracle为了加深理解记忆.只要是跟我的版本一样操作系统和oracle软件,按照步骤肯定可以成功安装 在虚拟机上安装Red Hat Enterprise L ...
- iOS 获取系统目录
//获取根目录 NSString *homePath = NSHomeDirectory(); NSLog(@"Home目录:%@",homePath); //获取Document ...
- jdbc select
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- hdu2393Higher Math
Problem Description You are building a house. You’d prefer if all the walls have a precise right ang ...