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; }
查看全文
相关阅读:
IPC之msgutil.c源码解读
IPC之msg.c源码解读
IPC之mqueue.c源码解读
从锅炉工到AI专家(10)
从锅炉工到AI专家(9)
从锅炉工到AI专家(8)
从锅炉工到AI专家(7)
从锅炉工到AI专家(6)
从锅炉工到AI专家(5)
从锅炉工到AI专家(4)
原文地址:https://www.cnblogs.com/jjtx/p/2533474.html
最新文章
01 Oracle——OracleXE与PLSQL的安装
38 bootstrap——徽章(常用在:未读消息)
37 bootstrap——小例子:下拉框
36 bootstrap——栅格系统练习
35 bootstrap——栅格系统
【shell脚本】将三个数字进行升序排序===numSort.sh
【shell脚本】创建账户及删除账户,批量创建账户及批量删除账户===autoCreateUser.sh
【VSFTP服务】vsftpd文件传输协议部署
【shell脚本】检测当前用户是否为超级管理员===checkRoot.sh
【shell脚本】定时备份日志===logBackup.sh
热门文章
【shell命令】常见系统变量($#、$*、$n分别表示的含义)
【shell脚本】通过位置变量创建Linux账户及密码===addUser.sh
【Linux命令】at、crontab(cronb)定时任务
【shell脚本】检查内存和磁盘使用情况===chenkMen.sh
【shell脚本】定时备份数据库===dbbackup.sh
IPC之util.c源码解读
IPC之syscall.c源码解读
IPC之shm.c源码解读
IPC之sem.c源码解读
IPC之namespace.c源码解读
Copyright © 2011-2022 走看看