DS实验题 融合软泥怪-1
题目
思路
很容易想到第一种做法,用Huffman算法,从森林中找出两个值最小的节点,合并再加入森林,在这个过程中不断记录。
但是每一次需要sort一遍,将最小的两个值节点置于头两个节点,最坏情况下复杂度是O(n^3)的量级,结果是TLE。
第二种方法是利用STL中的priority_queue模板进行解题,很方便,这里给出AC代码。但是优先队列手写实现还是需要实践,将在下一篇进行实现。
代码
Huffman:
//
// main.cpp
// Huffman Tree2
//
// Created by wasdns on 16/12/24.
// Copyright © 2016年 wasdns. All rights reserved.
//
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node
{
int num;
Node *l, *r;
};
bool cmp(Node *n1, Node *n2)
{
if (n1 -> num != n2 -> num) return n1 -> num < n2 -> num;
return false;
}
Node *node[100005];
/*
Ininode:初始化节点
*/
void Ininode(int n)
{
int i;
for (i = 1; i <= n; i++)
{
node[i] = new Node;
cin >> node[i] -> num;
node[i] -> l = NULL;
node[i] -> r = NULL;
}
}
/*
Huffman函数
*/
int Huffman(int n)
{
int cnt = 0;
sort(node+1, node+n+1, cmp);
while (1)
{
if (node[2] -> num == 100005) //只剩下一棵树
{
break;
}
cnt += node[1] -> num;
cnt += node[2] -> num;
node[1] -> num = node[1] -> num + node[2] -> num; //1、2合并为1
node[2] -> num = 100005; //删除2
sort(node+1, node+n+1, cmp); //将值最小的两棵树前移
}
return cnt;
}
int main()
{
int n;
cin >> n;
Ininode(n);
int ans = Huffman(n);
cout << ans << endl;
return 0;
}
STL_Priority_Queue:
//
// main.cpp
// PriorityQueue STL
//
// Created by wasdns on 16/12/24.
// Copyright © 2016年 wasdns. All rights reserved.
//
#include <iostream>
#include <functional>
#include <queue>
using namespace std;
struct cmp
{
bool operator() (int x,int y) {
return x > y;
}
};
int storage[100005];
int main()
{
int n;
cin >> n;
//priority_queue<int, vector<int>, greater<int>> q;
priority_queue<int, vector<int>, cmp> q;
for (int i = 1; i <= n; i++)
{
cin >> storage[i];
q.push(storage[i]);
}
int cnt = 0;
while (q.size() > 1)
{
int x = q.top();
q.pop();
int y = q.top();
q.pop();
cnt += x+y;
q.push(x+y);
}
cout << cnt << endl;
return 0;
}
2016/12/24
DS实验题 融合软泥怪-1的更多相关文章
- DS实验题 融合软泥怪-2 Heap实现
题目和STL实现:DS实验题 融合软泥怪-1 用堆实现优先队列 引言和堆的介绍摘自:Priority Queue(Heaps)--优先队列(堆) 引言: 优先队列是一个至少能够提供插入(Insert) ...
- DS实验题 Old_Driver UnionFindSet结构 指针实现邻接表存储
题目见前文:DS实验题 Old_Driver UnionFindSet结构 这里使用邻接表存储敌人之间的关系,邻接表用指针实现: // // main.cpp // Old_Driver3 // // ...
- DS实验题 Dijkstra算法
参考:Dijkstra算法 数据结构来到了图论这一章节,网络中的路由算法基本都和图论相关.于是在拿到DS的实验题的时候,决定看下久负盛名的Dijkstra算法. Dijkstra的经典应用是开放最短路 ...
- DS实验题 sights
算法与数据结构实验题 6.3 sights ★实验任务 美丽的小风姑娘打算去旅游散心,她走进了一座山,发现这座山有 n 个景点, 由于山路难修,所以施工队只修了最少条的路,来保证 n 个景点联通,娇弱 ...
- DS实验题 order
算法与数据结构 实验题 6.4 order ★实验任务 给出一棵二叉树的中序遍历和每个节点的父节点,求这棵二叉树的先序和后序遍历. ★数据输入 输入第一行为一个正整数n表示二叉树的节点数目,节点编号从 ...
- DS实验题 Order 已知父节点和中序遍历求前、后序
题目: 思路: 这题是比较典型的树的遍历问题,思路就是将中序遍历作为位置的判断依据,假设有个节点A和它的父亲Afa,那么如果A和Afa的顺序在中序遍历中是先A后Afa,则A是Afa的左儿子,否则是右儿 ...
- DS实验题 Inversion
题目: 解题过程: 第一次做这题的时候,很自然的想到了冒泡和选择,我交的代码是用选择写的.基本全WA(摊手). 贴上第一次的代码: // // main.cpp // sequenceschange ...
- DS实验题 Missile
题目: 提示:并没有精度问题. 原题 NOIP2010 导弹拦截 思路 设源点为A(x1, y1)和B(x2, y2). 第一步,用结构体存节点,包括以下元素: 1.横坐标x 2.纵坐标y 3.节点和 ...
- DS实验题 击鼓传花
题目: 代码1(数组实现): // // main.cpp // DS-击鼓传花 // // Created by wasdns on 16/11/9. // Copyright © 2016年 wa ...
随机推荐
- str.format
#使用str.format()函数 #使用'{}'占位符 print('I\'m {},{}'.format('Hongten','Welcome to my space!')) >>&g ...
- (原)android补间动画(四)之插补器Interpolator
比如说一段旋转动画 RotateAnimation animation = new RotateAnimation(0, 360, mMoveCircle.getMeasuredWidth() / 2 ...
- Python系统命令操作
系统命令 1.call 执行命令,返回状态码 ret = subprocess.call(['ls', '-l'], shell=False) ret = subprocess.call('ls -l ...
- PE440
一些证明,推荐复制入atom观看 首先我们考虑这个T(n)是什么,我们可以列出递归式: (definition:T) T(0) = 1 T(1) = 10 T(n) = 10*T(n-1) + T(n ...
- nodejs开发指南demo
由于手上拿的教程是2012年出版的,到如今已历经N个版本,所以在写代码时报过一堆错.这是解决错误后的版本. 源码下载
- Socket通信(一)
代码及PDF下载链接:http://download.csdn.net/detail/u010312811/9683034 例程1:实现服务器与客户端的连接与简单数据收发 参考链接:http://bl ...
- Mybatis 学习笔记1 不整合Spring的方式使用mybatis
两种方式都包含了: package com.test.mybatis; import java.util.List; import org.apache.ibatis.io.Resources; im ...
- uboot的编译
在我拿到开发板以后,uboot都是编译好的,但是我不知道它是如何生成uboot.bin文件的.经过一番摸索.我也会编译uboot了. #cd /home #mkdir study //创建工作目录 * ...
- 使用Python解析JSON数据的基本方法
这篇文章主要介绍了使用Python解析JSON数据的基本方法,是Python入门学习中的基础知识,需要的朋友可以参考下: ----------------------------------- ...
- javascript将object转string字符串
var jsonData = {a:1,b:2}; function obj2string(o) { var r = []; if (typeof o == "string") { ...