poj 3053 Fence Repair(优先队列)
题目链接:http://poj.org/problem?id=3253
思路分析:题目与哈夫曼编码原理相同,使用优先队列与贪心思想;读入数据在优先队列中,弹出两个数计算它们的和,再压入队列中;
代码如下:
#include <iostream>
#include <queue>
using namespace std; struct cmp
{
bool operator() (long long a, long long b)
{
return a > b;
}
}; int main()
{
priority_queue<long long, vector<long long>, cmp> Q;
long long n, ans = ; cin >> n;
for (int i = ; i < n; ++i)
{
long long num; cin >> num;
Q.push(num);
} long long a, b;
while (Q.size() != )
{
a = Q.top(); Q.pop();
b = Q.top(); Q.pop();
ans += a + b;
Q.push(a + b);
} cout << ans << endl; return ;
}
poj 3053 Fence Repair(优先队列)的更多相关文章
- 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 优先队列+贪心
Fence Repair Farmer John wants to repair a small length of the fence around the pasture. He measures ...
- poj 3253 Fence Repair (优先队列,哈弗曼)
题目链接:http://poj.org/problem?id=3253 题意:给出n块木板的长度L1,L2...Ln,求在一块总长为这个木板和的大木板中如何切割出这n块木板花费最少,花费就是将木板切割 ...
- poj 3253 Fence Repair(优先队列+huffman树)
一个很长的英文背景,其他不说了,就是告诉你锯一个长度为多少的木板就要花多少的零钱,把一块足够长(不是无限长)的木板锯成n段,每段长度都告诉你了,让你求最小花费. 明显的huffman树,优先队列是个很 ...
- 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 ...
- [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 Fence Repair(优先队列)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 51346 Accepted: 16857 De ...
随机推荐
- GDB调试之core文件(如何定位到Segment fault)
core dump又叫核心转储,当程序运行过程中发生异常,程序异常退出时,由操作系统把程序当前的内存状况存储在一个core文件中,叫core dump.(内部实现是:linux系统中内存越界会收到SI ...
- Assertion failure in -[UIView layoutSublayersOfLayer:]
Assertion failure in -[UIView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.137/UIView.m:8 ...
- C语言,函数的声明与定义
函数声明与定义 变量: 在讲变量前,先讲一下变量的声明和定义这两个概念. 声明一个变量,意味着向编译器描述变量的类型,但不为变量分配存储空间. 定义一个变量,意味着在声明变量的同时还要为变量分配存储空 ...
- HTML+CSS笔记 CSS中级 颜色&长度值
颜色值 在网页中的颜色设置是非常重要,有字体颜色(color).背景颜色(background-color).边框颜色(border)等,设置颜色的方法也有很多种: 1.英文命令颜色 语法: p{co ...
- 27_Blog Reader
这个App是用来读取 Official Google Blog 的内容,然后显示出来. 用了新建工程时用了 Master-Detail Application 这个模板.用了Core Data用来存储 ...
- SumoLogic
SumoLogic>>>Loggly. https://diyunpeng.loggly.com/setup MonitorWare http://www.monitorware.c ...
- QQwry
http://blog.runphp.net/archives/156 http://wenku.baidu.com/link?url=yvd3_EpMM66r6H4qkx3oqcaU2JZNAQv_ ...
- PSAM卡
PSAM卡 终端安全控制模块,符合<中国金融集成电路(IC卡)PSAM卡规范>, 包括普通PSAM卡和高速PSAM卡. PSAM符合以下标准及规范: 识别卡,带触点的集成电路卡标 ...
- AnyEvent::HTTP 实现异步请求
异步http: jrhmpt01:/root/async# cat a1.pl use LWP::UserAgent; use utf8; use DBI; use POSIX; use HTTP:: ...
- MySql学习笔记(转载)
. 数值类型 -- a. 整型 ---------- 类型 字节 范围(有符号位) tinyint 1字节 - ~ ...