01堆的定义
堆是一颗完全二叉树,树中每个结点的值都不小于(或不大于)其左右孩子结点的值。
大顶堆:父亲结点的值大于或等于孩子结点的值。
小顶堆:父亲结点的值小于或等于孩子结点的值。
建堆:给定一个初始序列,然后调整堆,在建堆的过程中,每次调整都是把结点从上往下调整,向下调整。
添加元素:把想要添加的元素放在数组的最后,然后向上调整。
堆排序:此时考虑大顶堆,递增排序的情况。
对于大顶堆来说,堆顶元素是最大的,在建堆完毕后,取出堆顶元素,将堆的最后一个元素替换至堆顶,再进行一次针对堆顶元素的向下调整。
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//堆排序
const int maxn = 100000;
int heap[maxn] = { 0 };//堆名
//堆的构建,小顶堆
void downAdjust(int low, int high) {
int i = low, j = 2 * i;// j 为 low 结点的叶子结点。
while (j <= high) {
//取最小的,小顶堆
if (j + 1 <= high && heap[j + 1] > heap[j]) {
j = j + 1;
}
if (heap[j] > heap[i]) {//选择最小的孩子结点,使根节点最小。
swap(heap[j], heap[i]);
i = j;//这一部分是因为上一步交换后,后面可能不符合根最小的要求,故还要往下调整,
j = i * 2;
}
else {
break;
}
}
}
void createHeap(int n) {
for (int i = n / 2; i >= 1; i--) {
downAdjust(i, n);//从下往上调整,跳过叶子结点
}
}
//建好了堆之后,还要进行堆排序
void heapSort(int n) {
createHeap(n);
for (int i = n; i > 1; i--) {
swap(heap[i], heap[1]);
downAdjust(1, i - 1);
}
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
cin >> heap[i];
}
createHeap(n);
heapSort(n);
for (int i = 1; i <= n; i++) {
printf("%d ", heap[i]);
}
printf("\n");
}
}
void insert(int x)
{
heap[++n]=x;
upAdjust(1,n);
}
//从当前往上调整。
void upAdjust(int low, int high) {
int i = high, j = i / 2;//i为欲调整的结点,j为其父亲
while (j >= low) {
if (heap[j] < heap[i]) {
swap(heap[j], heap[i]);
i = j;
j = i / 2;
}
else {
break;
}
}
}
问题 C: 合并果子(堆)
题目描述
在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。
每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。
因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
例如有3种果子,数目依次为1,2,9。可以先将 1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为 12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。
输入
输入文件fruit.in包括两行,第一行是一个整数n(1 <= n <= 30000),表示果子的种类数。第二行包含n个整数,用空格分隔,第i个整数ai(1 <= ai <= 20000)是第i种果子的数目。
输出
输出文件fruit.out包括一行,这一行只包含一个整数,也就是最小的体力耗费值。输入数据保证这个值小于231。
样例输入 Copy
10
3 5 1 7 6 4 2 5 4 1
样例输出 Copy
120
1、使用优先队列
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<algorithm>
#include<queue>
using namespace std;
/*
类似于构建哈夫曼树。
利用优先队列(本质是堆)。
将所有元素push进去优先队列中。(自定义小顶堆,不定义的话默认是大顶堆)
每次选择两个最小的元素出队,记录合并需要的能量,然后将合并的重量进队。
循环n - 1次。
*/
int n;
//优先队列,快忘光了。。。
struct node {
int data;
//谁小谁在top,自定义小顶堆
friend bool operator < (const node& a, const node& b) {
return a.data > b.data;
}
};
int main(){
while (scanf("%d", &n) != EOF) {
priority_queue<node>q;
node now;
for (int i = 0; i < n; i++) {
scanf("%d", &now.data);
q.push(now);
}
int ans = 0, a, b;
for (int i = 1; i <= n - 1; i++) {
//不过一般的队列取元素都是.front(),这里用的是top(),说明本质还是堆???
a = q.top().data; q.pop();
b = q.top().data; q.pop();
ans += a + b;
now.data = a + b;
q.push(now);//优先队列,加入其中自动排序。
}
printf("%d\n", ans);
}
}
2、使用大顶堆
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
//理解题意
const int maxn = 30010;
int a[maxn], b[maxn];
int heap[maxn];
//从当前往下调整。
void adjustHeap(int low, int high) {
int i = low, j = 2 * i;//i为欲调整的结点,
while (j <= high) {//!!!这里的等于还是需要的!!!
if (j + 1 < high && heap[j + 1] < heap[j]) {
j += 1;
}
if (heap[j] < heap[i]) {
swap(heap[j], heap[i]);
i = j;
j = j * 2;
}
else {
break;
}
}
}
void createHeap(int n) {
for (int i = n / 2; i >= 1; i--) {
adjustHeap(i, n);
}
}
void heapSort(int n) {
for (int i = n; i > 1; i--) {
swap(heap[i], heap[1]);
adjustHeap(1, i - 1);
}
}
//从当前往上调整。
void upAdjust(int low, int high) {
int i = high, j = i / 2;//i为欲调整的结点,j为其父亲
while (j >= low) {
if (heap[j] < heap[i]) {
swap(heap[j], heap[i]);
i = j;
j = i / 2;
}
else {
break;
}
}
}
int main(){
int n;
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; i++) {
cin >> heap[i];
}
createHeap(n);
heapSort(n);//堆排序出错!!!
//我觉得的可以得到一个从大到小的,使用最后两个,然后将得到的结果插入到堆排序中。
int sum = 0;//消费的总精力
int index = n;//堆的最后一个元素下标
//把前两个元素相加,得到的结果放在堆中重新计算。
int temp;
while (index > 1) {
temp = heap[index] + heap[index - 1];
sum += temp;
heap[index - 1] = temp; heap[index] = 0;//heap中减少一个元素。
upAdjust(1, index - 1);//heap应该时刻保持有序!向上调整新加入的结点。
//这个只是调整满足堆的情况?
heapSort(index - 1);//突然完全不懂堆排序了。。。。可能就没有懂过
//再使用堆排序??为什么不可以??
index--;
}
printf("%d\n", sum);
}
}