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; }
查看全文
相关阅读:
django的模型层
django的模版
SELinux入门基础
CentOS 7.5上部署Zabbix 3.4
使用orapki生成证书,配置Oracle数据使用SSL和TLS进行数据库连接的TCP/IP配置==TCPS
keepalived + LVS实现高可用负载均衡集群
Linux下LDAP用户认证,自动挂载网络共享文件系统
编译安装Nginx,配置使用HTTPS
RSyslog+MySQL+LogAnalyzer
Bash Shell脚本--复制程序(命令)到指定目录下,同时将其所依赖的库文件复制到对应的目录
原文地址:https://www.cnblogs.com/jjtx/p/2533474.html
最新文章
Backing up the tail
mysql 1449 : The user specified as a definer ('root'@'%') does not exist 解决方法
Spring整合Hessian访问远程服务
为啥要使用Hessian
MongoDB的使用学习之(七)MongoDB的聚合查询(两种方式)附项目源码
坚持
MongoDB的使用学习之(六)MongoDB的高级查询之条件操作符
MongoDB的使用学习之(五)Spring集成MongoDB以及简单的CRUD
马云:别在最能吃苦的年纪里选择了安逸!
2014-04-27 南江滨大道 6KM 晴
热门文章
锻炼计划
MongoDB的使用学习之(四)权限设置--用户名、密码、端口==
容器之 docker的监控平台(prometheus + Grafana)
容器 之搭建 jenkins ci 平台
容器之 镜像仓库搭建
容器之docker基础
jenkins的使用
gitlab安装
git的使用
django的模型层(二)
Copyright © 2011-2022 走看看