zoukankan
html css js c++ java
priority_queue POJ 3253 Fence Repair
#include <iostream> #include <queue> using namespace std; int main(void) { priority_queue<double> q; q.push(66.6); q.push(22.2); q.push(44.4); cout << q.top() << ' '; q.pop(); cout << q.top() << endl; q.pop(); q.push(11.1); q.push(55.5); q.push(33.3); q.pop(); while (!q.empty()) { cout << q.top() << ' '; q.pop(); } cout << endl; return 0; }
http://poj.org/problem?id=3253
1、O(n^2):for+Select()
超时
#define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; struct Node { long long weight; unsigned int parent; unsigned int lchild; unsigned int rchild; }; Node HT[40005] = {0}; long long Sum(const int &n) { long long sum = 0; for (int i = n + 1; i <= 2 * n - 1; i++) { sum += HT[i].weight; } return sum; } void Select(const int &len, int &a, int &b) { // 在HT[1..len]中选择parent为0且weight最小的两个结点, // 其序号分别为a和b。 for (int i = 1; i <= len; i++) { if (HT[i].parent == 0) { a = i; break; } } for (int i = a + 1; i <= len; i++) { if (HT[i].parent == 0) { b = i; break; } } if (HT[a].weight > HT[b].weight) // 保持a的始终小于b的,有序 swap(a, b); for (int i = max(a, b) + 1; i <= len; i++) { if (HT[i].parent == 0) { if (HT[i].weight > HT[b].weight) continue; else if (HT[i].weight < HT[a].weight) { b = a; a = i; } else b = i; } } if ( !(HT[a].parent == 0 && HT[b].parent == 0) ) { cerr << "Error!" << endl; exit(1); } } void CreatHuffmanTree(const int &n) { // weight存放n个字符的权值(均>0),构造哈夫曼树HT, // 并求出n个字符的哈夫曼编码HC for (int i = 1; i <= n; i++) cin >> HT[i].weight; for (int i = n + 1; i <= 2 * n - 1; i++) { // 在HT[1..i-1]中选择parent为0且weight最小的两个结点, // 其序号分别为s1和s2。 int s1 = 0; int s2 = 0; Select(i - 1, s1, s2); HT[s1].parent = i; HT[s2].parent = i; HT[i].lchild = s1; HT[i].rchild = s2; HT[i].weight = HT[s1].weight + HT[s2].weight; } } int main(void) { // freopen("cin.txt", "r", stdin); int n; cin >> n; if (n <= 1) cout << 0 << endl; else { CreatHuffmanTree(n); cout << Sum(n) << endl; } return 0; }
2、选k个最小值的算法用优先队列
参考:
#include <iostream> #include <queue> using namespace std; typedef long long ll; // 便于移植 int main(void) { priority_queue< ll, vector<ll>, greater<ll> > q; int N; cin >> N; for (int i = 0; i < N; i++) { ll x; cin >> x; q.push(x); } ll cost = 0; for (;;) { ll a = q.top(); q.pop(); if (q.empty()) break; ll b = q.top(); q.pop(); cost += a + b; q.push(a + b); } cout << cost << endl; return 0; }
查看全文
相关阅读:
vanilla单词的意思
快速排序实现
python下载一些包出现time out的问题解决
神经网络浅讲:从神经网络到深度学习
神经网络基础知识
TCP/IP
查找机器学习相关数据集
[数据治理]
【算法】——LRU
【大数据开发工程师】面试——HBase
原文地址:https://www.cnblogs.com/jjtx/p/2533474.html
最新文章
第一次,发一个试试
WPS excel 操作,组件服务里找不到wps组件, 程序报:检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败
tasklist, 查找进程,杀掉进程
redis-cli --cluster create 群集
查出当前数据库,所有数据表索引碎片情况。
windows 2012 R2 系统下, redis 主从配置
mysql 5.7*, 报错:this is incompatible with DISTINCT
[EndOfStreamException: 尝试读取超出流末尾的内容。]
.net MVC 中,使用 BundleConfig 合并js文件后,网站无法运行,发现代码丢失问题。
Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after
热门文章
Windows service 安装命令
你只管走你的路
Mybatis_plus
初探SpringBoot
初探JVM
SpringMVC学习(二)
SpringMVC
interesting
求大数阶乘
数字签名和数字证书
Copyright © 2011-2022 走看看