优先队列 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 ...
随机推荐
- OpenGL之路(五)制作旋转飞机模型
#include <gl/glut.h> #include <gl/GLU.h> #include <gl/GL.h> #pragma comment(lib, & ...
- 尝试使用UISearchDisplayController及对苹果对控件封装习惯的理解
本文转载至 http://blog.sina.com.cn/s/blog_74e9d98d01019vji.html 在之前做过的应用中,很多都有“搜索”这个功能,大部分情况下我都是只采用UISe ...
- strncpy和strlen的可能的实现
#include <stdio.h> #include <stdlib.h>//为避免与标准库中的函数发生混淆,我将它们命名为stringNCopy和stringLength ...
- poj 2559 Largest Rectangle in a Histogram 栈
// poj 2559 Largest Rectangle in a Histogram 栈 // // n个矩形排在一块,不同的高度,让你求最大的矩形的面积(矩形紧挨在一起) // // 这道题用的 ...
- log4j 路径环境变量配置和log4j加载配置
1.lo4j日志路径从环境变量读取,log4j.xml配置如下: 具体配置如下: log4j.appender.R.Encoding=UTF-8 log4j.appender.R=org.apache ...
- 使用proc接口例子【转】
本文转载自:http://blog.csdn.net/mike8825/article/details/52434666 版权声明:本文为博主原创文章,未经博主允许不得转载. 在上一篇的使用sys接口 ...
- YTU 2391: 求素数
2391: 求素数 时间限制: 1 Sec 内存限制: 128 MB 提交: 116 解决: 3 题目描述 设计一个程序,输出所有小于等于n(n为一个大于2的正整数)的素数. 要求:(1)每行输出 ...
- Java数据库操作类演示
只在mysql上测试过,不知道算不算好使1. [代码][Java]代码 package org.load.demo; import java.io.IOException;import ja ...
- codeforces 459 B.Pashmak and Flowers 解题报告
题目链接:http://codeforces.com/problemset/problem/459/B 题目意思:有 n 朵 flowers,每朵flower有相应的 beauty,求出最大的beau ...
- UVA-10391(字符串检索)
题意: 给定一个字典,要求找出所有的复合词; 思路: 用map把词都存起来,再把词拆开看是否是出现过的单词; AC代码: #include <bits/stdc++.h> #include ...