POJ3253 Haffman

POJ3253

分析:

简单的哈弗曼树的应用。

AC代码:

 1 //Memory: 316K        Time: 16MS
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <queue>
 6
 7 using namespace std;
 8
 9 int n;
10 int l;
11 priority_queue <int, vector<int>, greater<int>> q;
12
13 int main()
14 {
15     while ( !q.empty() ) q.pop();
16     scanf("%d", &n);
17     for (int i = 0; i < n; i++) {
18         scanf("%d", &l);
19         q.push(l);
20     }
21     long long ans = 0;
22     while ( q.size() >= 2 ) {
23         int a = q.top();
24         q.pop();
25         int b = q.top();
26         q.pop();
27         ans += (a + b);
28         q.push(a + b);
29     }
30     printf("%lld\n", ans);
31     return 0;
32 }

POJ3253 Haffman

时间: 2024-10-14 09:22:22

POJ3253 Haffman的相关文章

Haffman算法(C++)

Huffman编码,C++实现,只是为了说明大致的思路,还有很多不完美之处,比如在输入数据超出限制等条件下会出现错误. 1 #include<iostream> 2 #include<string> 3 using namespace std; 4 #define MAX 20 5 6 /* 7 ** 用二叉树表示的Huffman节点 8 */ 9 typedef struct NodeTag { 10 char c; // 字母 11 int weight; // 频率 12 s

poj3253

/** \brief poj 3253 * * \param date 2014/8/8 * \param state AC * \return memory 1124K time 125ms * */ #include <iostream> #include <fstream> #include <queue> #include <functional> using namespace std; struct number { //int x; __int

poj3253 Fence Repair STL优先队列

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://poj.org/problem?id=3253 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

数据结构:haffman树

#include <iostream> using namespace std; struct haffNode { int data; haffNode *left; haffNode *right; haffNode *parent; haffNode(int d = int()) :data(d), left(NULL), right(NULL), parent(NULL){} bool operator>(const haffNode &node) { //重载>,

哈夫曼算法(haffman)实现压缩和解压缩-C语言实现

/* * ===================================================================================== * * Filename: haffman.c * * Description: huffman coder decoder * * Version: 1.0 * Created: * Revision: none * Compiler: gcc * * * =============================

poj3253哈夫曼树

Fence Repair Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Farmer John wants to repair a small length of the fence around the pasture. He measures the

java实现Haffman编码

1.先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下 package com.hjp.huffman; /** * Created by JiaPeng on 2016/12/28. */ public class Node<T> implements Comparable<Node<T>> { private T data; private int weight; private Node<T>

POJ3253 Fence Repair 小顶堆+贪心

给了你N个木棒,求把他们组装成一根需要的最小花费,每次只能选两根组装在一起,需要的花费为两个木棒之和, 以前遇到过把一整根切开的,那个是DP,这个则有些类似,可是大胆的猜测了一下,直接每次选取所有木棒中最短的两根,这样就可以了,那么贪心是适用的,但是数量很多,而且两根最短的组装好了得插回去,这样不可能每次都排序吧, 这题首先优先队列肯定是可以做的, 最小堆也是可以的,每次都选出堆里的最小的两个求和再放回去即可 队列本身也就是堆吧,所以差别不大,但是没用过最小堆最大堆的 所以用一次把 #inclu

Haffman Code

什么是Haffman编码?以后再说,嘿嘿先上实现. Python 实现: 在这个实现当中,我们并没有统计文本的字符出现频率,仅仅是为Haffman Coding的一个demo. """ Code writer : EOF Code file : haffman.py Code date : 2015.01.28 e-mail : [email protected] Code description: Here is a implementation of Haffman-Co