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; }
查看全文
相关阅读:
bzoj 2744 朋友圈
bzoj 3674 可持久化并查集加强版
3.15 模拟赛
luogu 4720 【模板】扩展卢卡斯
洛谷 P1447 [NOI2010]能量采集 (莫比乌斯反演)
洛谷P2522 [HAOI2011]Problem b (莫比乌斯反演+容斥)
7-12 与零交换 (25 分)
理想的正方形(单调队列)
Flowerpot(单调队列)
生日礼物(单调队列)
原文地址:https://www.cnblogs.com/jjtx/p/2533474.html
最新文章
20200413(DEF)题解 by 孙晨曦
20200414(ABC)题解 by 马鸿儒 苏用
20200413(DE)题解 by 孙晨曦
20200417(ABC)题解 by 马鸿儒
20200416(AB) 题解 by 马鸿儒
20200419(ABC)题解 by 马鸿儒,孙晨曦
为什么Map桶中个数超过8才转为红黑树
java打包上传服务器的一些命令
redis 记录
百度之星初赛签到(1001/1005)
热门文章
java8 lambda表达式
Linux环境下安装mysql(远程连接),zookeeper,java,tomcat.
HDU6383p1m2(二分)
连接远程mysql(Linux环境)
多校 HDU
数对(数学)
3.27 模拟赛
3.22 模拟赛
bzoj 4521 电话号码
【系列】 莫比乌斯反演
Copyright © 2011-2022 走看看