【算法笔记自学】第 9 章 提高篇(3)——数据结构专题(2)

9.1树与二叉树

#include <cstdio>int main() {int n, m;scanf("%d%d", &n, &m);printf(n == m + 1 ? "Yes" : "No");return 0;
}

9.2二叉树的遍历

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre;void preOrder(int root) {if (root == -1) {return;}pre.push_back(root);preOrder(nodes[root].l);preOrder(nodes[root].r);
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}preOrder(0);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre;void preOrder(int root) {if (root == -1) {return;}preOrder(nodes[root].l);pre.push_back(root);preOrder(nodes[root].r);
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}preOrder(0);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre;void preOrder(int root) {if (root == -1) {return;}preOrder(nodes[root].l);preOrder(nodes[root].r);pre.push_back(root);
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}preOrder(0);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> layer;void layerOrder(int root) {queue<int> q;q.push(root);while (!q.empty()) {int front = q.front();q.pop();layer.push_back(front);if (nodes[front].l != -1) {q.push(nodes[front].l);}if (nodes[front].r != -1) {q.push(nodes[front].r);}}
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}layerOrder(0);for (int i = 0; i < (int)layer.size(); i++) {printf("%d", layer[i]);if (i < (int)layer.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];int getHeight(int root) {if (root == -1) {return 0;}int leftHeight = getHeight(nodes[root].l);int rightHeight = getHeight(nodes[root].r);return max(leftHeight, rightHeight) + 1;
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}printf("%d", getHeight(0));return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];int layers[MAXN];void layerOrder(int root) {queue<int> q;q.push(root);int layer = 1;while (!q.empty()) {int cnt = q.size();for (int i = 0; i < cnt; i++) {int front = q.front();q.pop();layers[front] = layer;if (nodes[front].l != -1) {q.push(nodes[front].l);}if (nodes[front].r != -1) {q.push(nodes[front].r);}}layer++;}
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}layerOrder(0);for (int i = 0; i < n; i++) {printf("%d", layers[i]);if (i < n - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre, in, post;int buildTree(int preL, int preR, int inL, int inR) {if (preL > preR) {return -1;}int root = pre[preL];int inIndexOfRoot;for (int i = inL; i <= inR; i++) {if (in[i] == root) {inIndexOfRoot = i;break;}}int leftCount = inIndexOfRoot - inL;nodes[root].l = buildTree(preL + 1, preL + leftCount, inL, inIndexOfRoot - 1);nodes[root].r = buildTree(preL + leftCount + 1, preR, inIndexOfRoot + 1, inR);return root;
}void postOrder(int root) {if (root == -1) {return;}postOrder(nodes[root].l);postOrder(nodes[root].r);post.push_back(root);
}int main() {int n, x;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &x);pre.push_back(x);}for (int i = 0; i < n; i++) {scanf("%d", &x);in.push_back(x);}int root = buildTree(0, n - 1, 0, n - 1);postOrder(root);for (int i = 0; i < (int)post.size(); i++) {printf("%d", post[i]);if (i < (int)post.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre, in, post;int buildTree(int postL, int postR, int inL, int inR) {if (postL > postR) {return -1;}int root = post[postR];int inIndexOfRoot;for (int i = inL; i <= inR; i++) {if (in[i] == root) {inIndexOfRoot = i;break;}}int leftCount = inIndexOfRoot - inL;nodes[root].l = buildTree(postL, postL + leftCount - 1, inL, inIndexOfRoot - 1);nodes[root].r = buildTree(postL + leftCount, postR - 1, inIndexOfRoot + 1, inR);return root;
}void preOrder(int root) {if (root == -1) {return;}pre.push_back(root);preOrder(nodes[root].l);preOrder(nodes[root].r);
}int main() {int n, x;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &x);post.push_back(x);}for (int i = 0; i < n; i++) {scanf("%d", &x);in.push_back(x);}int root = buildTree(0, n - 1, 0, n - 1);preOrder(root);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <map>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int l, r;
} nodes[MAXN];vector<int> pre, in, layer;int buildTree(vector<int> layer, int inL, int inR) {if (layer.empty()) {return -1;}int root = layer[0];map<int, bool> isLeft;int inIndexOfRoot;for (int i = inL; i <= inR; i++) {if (in[i] == root) {inIndexOfRoot = i;break;} else {isLeft[in[i]] = true;}}vector<int> leftLayer, rightLayer;for (int i = 1; i < layer.size(); i++) {if (isLeft[layer[i]]) {leftLayer.push_back(layer[i]);} else {rightLayer.push_back(layer[i]);}}nodes[root].l = buildTree(leftLayer, inL, inIndexOfRoot - 1);nodes[root].r = buildTree(rightLayer, inIndexOfRoot + 1, inR);return root;
}void preOrder(int root) {if (root == -1) {return;}pre.push_back(root);preOrder(nodes[root].l);preOrder(nodes[root].r);
}int main() {int n, x;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &x);layer.push_back(x);}for (int i = 0; i < n; i++) {scanf("%d", &x);in.push_back(x);}int root = buildTree(layer, 0, n - 1);preOrder(root);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int data;int l, r;
} nodes[MAXN];int treePathSum = 0;void getTreePathSum(int root, int nodePathSum) {if (root == -1) {return;}nodePathSum += nodes[root].data;if (nodes[root].l == -1 && nodes[root].r == -1) {treePathSum += nodePathSum;} else {getTreePathSum(nodes[root].l, nodePathSum);getTreePathSum(nodes[root].r, nodePathSum);}
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &nodes[i].data);}for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}getTreePathSum(0, 0);printf("%d", treePathSum);return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int data;int l, r;
} nodes[MAXN];int treeWeightedPathLength = 0;void getTreeWeightedPathLength(int root, int nodePathLength) {if (root == -1) {return;}if (nodes[root].l == -1 && nodes[root].r == -1) {treeWeightedPathLength += nodes[root].data * nodePathLength;} else {nodePathLength++;getTreeWeightedPathLength(nodes[root].l, nodePathLength);getTreeWeightedPathLength(nodes[root].r, nodePathLength);}
}int main() {int n;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &nodes[i].data);}for (int i = 0; i < n; i++) {scanf("%d%d", &nodes[i].l, &nodes[i].r);}getTreeWeightedPathLength(0, 0);printf("%d", treeWeightedPathLength);return 0;
}

9.3树的遍历

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> pre;void preOrder(int root) {pre.push_back(root);for (int i = 0; i < nodes[root].children.size(); i++) {preOrder(nodes[root].children[i]);}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}preOrder(0);for (int i = 0; i < pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> post;void postOrder(int root) {for (int i = 0; i < nodes[root].children.size(); i++) {postOrder(nodes[root].children[i]);}post.push_back(root);
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}postOrder(0);for (int i = 0; i < post.size(); i++) {printf("%d", post[i]);if (i < (int)post.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
#include <queue>
using namespace std;const int MAXN = 50;struct Node {vector<int> children;
} nodes[MAXN];vector<int> layer;void layerOrder(int root) {queue<int> q;q.push(root);while (!q.empty()) {int front = q.front();q.pop();layer.push_back(front);for (int i = 0; i < nodes[front].children.size(); i++) {q.push(nodes[front].children[i]);}}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}layerOrder(0);for (int i = 0; i < layer.size(); i++) {printf("%d", layer[i]);if (i < (int)layer.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int data;vector<int> children;
} nodes[MAXN];int treePathSum = 0;void getTreePathSum(int root, int nodePathSum) {nodePathSum += nodes[root].data;if (nodes[root].children.empty()) {treePathSum += nodePathSum;}for (int i = 0; i < nodes[root].children.size(); i++) {getTreePathSum(nodes[root].children[i], nodePathSum);}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &nodes[i].data);}for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}getTreePathSum(0, 0);printf("%d", treePathSum);return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int data;vector<int> children;
} nodes[MAXN];int treePathLength = 0;void getTreePathLength(int root, int edgeCount) {if (nodes[root].children.empty()) {treePathLength += nodes[root].data * edgeCount;}for (int i = 0; i < nodes[root].children.size(); i++) {getTreePathLength(nodes[root].children[i], edgeCount + 1);}
}int main() {int n, k, child;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &nodes[i].data);}for (int i = 0; i < n; i++) {scanf("%d", &k);for (int j = 0; j < k; j++) {scanf("%d", &child);nodes[i].children.push_back(child);}}getTreePathLength(0, 0);printf("%d", treePathLength);return 0;
}

9.4二叉查找树(BST)

#include <cstdio>
#include <vector>
using namespace std;const int MAXN = 50;struct Node {int data;int l, r;
} nodes[MAXN];int nodeCount = 0;int newNode(int data) {nodes[nodeCount].data = data;nodes[nodeCount].l = nodes[nodeCount].r = -1;return nodeCount++;
}int insert(int root, int data) {if (root == -1) {return newNode(data);}if (data < nodes[root].data) {nodes[root].l = insert(nodes[root].l, data);} else {nodes[root].r = insert(nodes[root].r, data);}return root;
}vector<int> pre;void preOrder(int root) {if (root == -1) {return;}pre.push_back(nodes[root].data);preOrder(nodes[root].l);preOrder(nodes[root].r);
}int main() {int n, data, root = -1;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &data);root = insert(root, data);}preOrder(root);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
using namespace std;vector<int> in;bool isBST() {for (int i = 1; i < in.size(); i++) {if (in[i] <= in[i - 1]) {return false;}}return true;
}int main() {int n, x;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &x);in.push_back(x);}printf(isBST() ? "Yes" : "No");return 0;
}

9.5平衡二叉树(AVL树)

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int data;int height;int l, r;
} nodes[MAXN];int nodeCount = 0;int newNode(int data) {nodes[nodeCount].data = data;nodes[nodeCount].height = 1;nodes[nodeCount].l = nodes[nodeCount].r = -1;return nodeCount++;
}int getHeight(int root) {if (root == -1) {return 0;} else {return nodes[root].height;}
}void updateHeight(int root) {nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}int getBalanceFactor(int root) {return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}int insert(int root, int data) {if (root == -1) {return newNode(data);}if (data < nodes[root].data) {nodes[root].l = insert(nodes[root].l, data);} else {nodes[root].r = insert(nodes[root].r, data);}updateHeight(root);return root;
}vector<int> balanceFactor;void inOrder(int root) {if (root == -1) {return;}inOrder(nodes[root].l);balanceFactor.push_back(getBalanceFactor(root));inOrder(nodes[root].r);
}int main() {int n, data, root = -1;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &data);root = insert(root, data);}inOrder(root);for (int i = 0; i < (int)balanceFactor.size(); i++) {printf("%d", balanceFactor[i]);if (i < (int)balanceFactor.size() - 1) {printf(" ");}}return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int data;int height;int l, r;
} nodes[MAXN];int nodeCount = 0;int newNode(int data) {nodes[nodeCount].data = data;nodes[nodeCount].height = 1;nodes[nodeCount].l = nodes[nodeCount].r = -1;return nodeCount++;
}int getHeight(int root) {if (root == -1) {return 0;} else {return nodes[root].height;}
}void updateHeight(int root) {nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}int getBalanceFactor(int root) {return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}int insert(int root, int data) {if (root == -1) {return newNode(data);}if (data < nodes[root].data) {nodes[root].l = insert(nodes[root].l, data);} else {nodes[root].r = insert(nodes[root].r, data);}updateHeight(root);return root;
}bool isAVL(int root) {if (root == -1) {return true;}return isAVL(nodes[root].l) && isAVL(nodes[root].r) && abs(getBalanceFactor(root)) <= 1;
}int main() {int n, data, root = -1;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &data);root = insert(root, data);}printf(isAVL(root) ? "Yes" : "No");return 0;
}

#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 50;struct Node {int data;int height;int l, r;
} nodes[MAXN];int nodeCount = 0;int newNode(int data) {nodes[nodeCount].data = data;nodes[nodeCount].height = 1;nodes[nodeCount].l = nodes[nodeCount].r = -1;return nodeCount++;
}int getHeight(int root) {if (root == -1) {return 0;} else {return nodes[root].height;}
}void updateHeight(int root) {nodes[root].height = max(getHeight(nodes[root].l), getHeight(nodes[root].r)) + 1;
}int getBalanceFactor(int root) {return getHeight(nodes[root].l) - getHeight(nodes[root].r);
}int L(int root) {int temp = nodes[root].r;nodes[root].r = nodes[temp].l;nodes[temp].l = root;updateHeight(root);updateHeight(temp);return temp;
}int R(int root) {int temp = nodes[root].l;nodes[root].l = nodes[temp].r;nodes[temp].r = root;updateHeight(root);updateHeight(temp);return temp;
}int insert(int root, int data) {if (root == -1) {return newNode(data);}if (data < nodes[root].data) {nodes[root].l = insert(nodes[root].l, data);updateHeight(root);if (getBalanceFactor(root) == 2) {if (getBalanceFactor(nodes[root].l) == 1) {root = R(root);} else if (getBalanceFactor(nodes[root].l) == -1) {nodes[root].l = L(nodes[root].l);root = R(root);}}} else {nodes[root].r = insert(nodes[root].r, data);updateHeight(root);if (getBalanceFactor(root) == -2) {if (getBalanceFactor(nodes[root].r) == -1) {root = L(root);} else if (getBalanceFactor(nodes[root].r) == 1) {nodes[root].r = R(nodes[root].r);root = L(root);}}}return root;
}vector<int> pre;void preOrder(int root) {if (root == -1) {return;}pre.push_back(nodes[root].data);preOrder(nodes[root].l);preOrder(nodes[root].r);
}int main() {int n, data, root = -1;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &data);root = insert(root, data);}preOrder(root);for (int i = 0; i < (int)pre.size(); i++) {printf("%d", pre[i]);if (i < (int)pre.size() - 1) {printf(" ");}}return 0;
}

9.6并查集

#include <cstdio>
#include <cstring>const int MAXN = 100;
int father[MAXN];int findFather(int x) {int xCopy = x;while (father[x] != x) {x = father[x];}int root = x;x = xCopy;while (father[x] != x) {int fatherX = father[x];father[x] = root;x = fatherX;}return root;
}void unionSet(int a, int b) {int faA = findFather(a);int faB = findFather(b);if (faA != faB) {father[faA] = faB;}
}void init(int n) {for (int i = 0; i < n; i++) {father[i] = i;}
}int main() {int n, m, a, b;scanf("%d%d", &n, &m);init(n);for (int i = 0; i < m; i++) {scanf("%d%d", &a, &b);unionSet(a - 1, b - 1);}int classCount = 0;for (int i = 0; i < n; i++) {if (father[i] == i) {classCount++;}}printf("%d", classCount);return 0;
}

#include <cstdio>
#include <cstring>const int MAXN = 100;
int father[MAXN];int findFather(int x) {int xCopy = x;while (father[x] != x) {x = father[x];}int root = x;x = xCopy;while (father[x] != x) {int fatherX = father[x];father[x] = root;x = fatherX;}return root;
}void unionSet(int a, int b) {int faA = findFather(a);int faB = findFather(b);if (faA != faB) {father[faA] = faB;}
}void init(int n) {for (int i = 0; i < n; i++) {father[i] = i;}
}int main() {int n, m, a, b;scanf("%d%d", &n, &m);init(n);for (int i = 0; i < m; i++) {scanf("%d%d", &a, &b);unionSet(a - 1, b - 1);}bool linked = true;for (int i = 1; i < n; i++) {if (findFather(i) != findFather(0)) {linked = false;}}printf(linked ? "Yes" : "No");return 0;
}
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;const int MAXN = 100;
int father[MAXN];
int score[MAXN];vector<int> classes;int findFather(int x) {int xCopy = x;while (father[x] != x) {x = father[x];}int root = x;x = xCopy;while (father[x] != x) {int fatherX = father[x];father[x] = root;x = fatherX;}return root;
}void unionSet(int a, int b) {int faA = findFather(a);int faB = findFather(b);if (faA != faB) {if (score[faA] < score[faB]) {father[faA] = faB;} else {father[faB] = faA;}}
}void init(int n) {for (int i = 0; i < n; i++) {father[i] = i;}
}int main() {int n, m, a, b;scanf("%d%d", &n, &m);init(n);for (int i = 0; i < n; i++) {scanf("%d", &score[i]);}for (int i = 0; i < m; i++) {scanf("%d%d", &a, &b);unionSet(a - 1, b - 1);}for (int i = 0; i < n; i++) {if (findFather(i) == i) {classes.push_back(score[i]);}}sort(classes.rbegin(), classes.rend());printf("%d\n", (int)classes.size());for (int i = 0; i < classes.size(); i++) {printf("%d", classes[i]);if (i < (int)classes.size() - 1) {printf(" ");}}return 0;
}

9.7堆

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 1000 + 1;
int heap[MAXN];void downAdjust(int low, int high) {int i = low, j = i * 2;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);}
}int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &heap[i]);}createHeap(n);for (int i = 1; i <= n; i++) {printf("%d", heap[i]);if (i < n) {printf(" ");}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 1000 + 1;
int heap[MAXN];void downAdjust(int low, int high) {int i = low, j = i * 2;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);}
}int deleteTop(int n) {if (n > 0) {heap[1] = heap[n--];downAdjust(1, n);}return n;
}int main() {int n;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &heap[i]);}createHeap(n);n = deleteTop(n);for (int i = 1; i <= n; i++) {printf("%d", heap[i]);if (i < n) {printf(" ");}}return 0;
}

#include <cstdio>
#include <algorithm>
using namespace std;const int MAXN = 50 + 1;
int heap[MAXN];void downAdjust(int low, int high) {int i = low, j = i * 2;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;scanf("%d", &n);for (int i = 1; i <= n; i++) {scanf("%d", &heap[i]);}heapSort(n);for (int i = 1; i <= n; i++) {printf("%d", heap[i]);if (i < n) {printf(" ");}}return 0;
}

9.8哈夫曼树

#include <iostream>
#include <vector>
#include <queue>using namespace std;int minCostToMergeFruits(vector<int>& fruits) {// 使用优先队列(最小堆)来处理priority_queue<int, vector<int>, greater<int>> minHeap(fruits.begin(), fruits.end());int totalCost = 0;// 当堆中还有超过一个元素时,进行合并操作while (minHeap.size() > 1) {// 取出最小的两个果堆int first = minHeap.top();minHeap.pop();int second = minHeap.top();minHeap.pop();// 合并这两个果堆int mergedCost = first + second;totalCost += mergedCost;// 将新的合并后的果堆放回堆中minHeap.push(mergedCost);}return totalCost;
}int main() {int n;cin >> n;vector<int> fruits(n);for (int i = 0; i < n; ++i) {cin >> fruits[i];}int result = minCostToMergeFruits(fruits);cout << result << endl;return 0;
}

#include <cstdio>
#include <queue>
using namespace std;priority_queue<int, vector<int>, greater<int> > pq;int main() {int n, weight;scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%d", &weight);pq.push(weight);}int ans = 0;while (pq.size() > 1) {int top1 = pq.top();pq.pop();int top2 = pq.top();pq.pop();pq.push(top1 + top2);ans += top1 + top2;}printf("%d", ans);return 0;
}

#include <iostream>
#include <string>
#include <queue>
using namespace std;const int MAXC = 26;
int charCnt[MAXC] = {0};priority_queue<int, vector<int>, greater<int> > pq;int main() {string s;cin >> s;for (int i = 0; i < s.length(); i++) {charCnt[s[i] - 'A']++;}for (int i = 0; i < MAXC; i++) {if (charCnt[i] > 0) {pq.push(charCnt[i]);}}int ans = 0;while (pq.size() > 1) {int top1 = pq.top();pq.pop();int top2 = pq.top();pq.pop();pq.push(top1 + top2);ans += top1 + top2;}printf("%d", ans);return 0;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/diannao/45700.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java毕业设计 基于SSM vue电影订票系统小程序 微信小程序

Java毕业设计 基于SSM vue电影订票系统小程序 微信小程序 SSM 电影订票系统小程序 功能介绍 用户 登录 注册 忘记密码 首页 图片轮播 电影信息 电影详情 评论 收藏 预订 电影资讯 资讯详情 用户信息修改 电影评价 我的收藏管理 用户充值 在线客服 我的订单 管理员 登录 个人…

不遵守全局主键配置【PGSQL】

<selectKey keyProperty"id" resultType"java.lang.Long" order"BEFORE"> SELECT nextval(ft.id2_seq::regclass) </selectKey> 这段 XML 配置中的 <selectKey> 标签是 MyBatis 中用来生成主键的关键部分。让我解释一下每个…

SpringCloud集成kafka集群

目录 1.引入kafka依赖 2.在yml文件配置配置kafka连接 3.注入KafkaTemplate模版 4.创建kafka消息监听和消费端 5.搭建kafka集群 5.1 下载 kafka Apache KafkaApache Kafka: A Distributed Streaming Platform.https://kafka.apache.org/downloads.html 5.2 在config目录下做…

java导出word实现

参考&#xff1a;Poi-tl Documentation

NaiveUI与ElementUI 比较分析

前言 在前端开发的广阔领域中&#xff0c;Vue.js作为最流行的前端框架之一&#xff0c;为开发者提供了丰富的组件库&#xff0c;其中NaiveUI和ElementUI是两个备受瞩目的选择。本文将深入分析这两个组件库的特点、优劣势以及适用场景&#xff0c;帮助开发者在项目中做出更合适…

全渠道AI智能商品管理软件平台 助力零售品牌占领技术高地

关于7thonline第七在线 1999年创立于纽约&#xff0c;7thonline第七在线全渠道AI智能商品管理平台&#xff0c;以先进的数学算法模型、人工智能和机器学习技术为核心驱动力&#xff0c;融合了众多零售商品管理的卓越实践经验&#xff0c;精心打造出一套深度适配零售业务场景的自…

[每周一更]-(第105期):SSL证书过期后引发的DNS缓存问题

问题回顾&#xff1a; ​ 上班路上收到ZeroSSL邮件通知我们清点项目的SSL证书到期了&#xff0c;到公司还是登录网址查看信息&#xff0c;一看果然是7.10也就是今天到期&#xff0c;开始看下acme.sh的定制任务为何没生效&#xff0c;一看crontab脚本&#xff0c;日志任务丢垃圾…

tomcat和nginx实现动静分离

访问nginx就是静态页面&#xff0c;nginx代理index.jsp可以访问tomcat的动态页面。 实验 1、设备以及IP地址 nginx1 192.168.10.41 tomcat1 192.168.10.51 tomcat2 192.168.10.52 2、tomcat1 的配置 创建动态页面 cd /usr/local/tomcat/webapps 创建一个目录作为一个ser…

复现ORB3-YOLO8项目记录

文章目录 1.编译错误1.1 错误11.2 错误21.3 错误31.4 错误4 1.编译错误 首先ORB-SLAM相关项目已经写过很多篇博客了&#xff0c;从ORB-SLAM2怎么运行&#xff0c;再到现在的项目。关于环境已经不想多说了 1.1 错误1 – DEPENDENCY_LIBS : /home/lvslam/ORB3-YOLO8/Thirdparty…

2024年C#优秀实用的类库推荐

简介 在软件开发领域&#xff0c;随着技术的不断进步和项目的复杂化&#xff0c;使用高质量的类库来加速开发过程、减少错误并提高代码质量变得至关重要。C# 作为一款功能强大的编程语言&#xff0c;拥有众多优秀的类库供开发者选择。本文旨在为您推荐一些在2024年备受推崇的C…

Feature Alignment and Uniformity for Test Time Adaptation--论文笔记

论文笔记 资料 1.代码地址 https://github.com/SakurajimaMaiii/TSD 2.论文地址 https://arxiv.org/abs/2303.10902 3.数据集地址 论文摘要的翻译 TTA在接收训练分布外的测试域样本时对深度神经网络进行自适应。在这样设置下&#xff0c;模型只能访问在线未标记的测试样…

昇思MindSpore学习总结十五 ——基于Mindspore 实现BERT对话情绪识别

1、环境配置 根据实际情况&#xff0c;选择合适版本。 %%capture captured_output # 实验环境已经预装了mindspore2.2.14&#xff0c;如需更换mindspore版本&#xff0c;可更改下面mindspore的版本号 !pip uninstall mindspore -y !pip install -i https://pypi.mirrors.ustc…

MYSQL--第七次作业

MYSQL–第七次作业 在product表上创建三个触发器。每次激活触发器后&#xff0c;都会更新operate表。product表和表的内容如下 Product表内容 字段名 字段描述 数据类型 主键 外键 非空 唯一 自增 Id 产品编号 Int(10) 是 否 是 是 否 Name …

torch之从.datasets.CIFAR10解压出训练与测试图片 (附带网盘链接)

前言 从官网上下载的是长这个样子的 想看图片&#xff0c;咋办咧&#xff0c;看下面代码 import torch import torchvision import numpy as np import os import cv2 batch_size 50transform_predict torchvision.transforms.Compose([torchvision.transforms.ToTensor(),…

ubuntu 24.04 安装telnet服务

1.安装telnet客户端 $sudo apt-get install telnet 2.安装telnet服务器 $sudo apt-get instlal telnetd 3.安装网络守护进程服务程序来管理telnet服务 $sudo apt-get install xinetd 4.修改配置文件inetd.conf $sudo vi /etc/inetd.conf telnet stream tcp nowait telne…

echarts解决数据差异过大的问题

问题描述 使用echarts折线图和柱状图展示数据时&#xff0c;如果数据差异值较大&#xff0c;会导致显示图形差异过大&#xff0c;图表不美观。 如这一组数据[2000000, 200, 0.1, 20, 0, -10, -3000]&#xff0c;渲染出来的效果如下图&#xff1a; 可以看到由于最大值和最小值差…

几何距离与函数距离:解锁数据空间中的奥秘

几何距离&#xff1a;直观的空间度量 几何距离&#xff0c;顾名思义&#xff0c;是我们在几何学中熟悉的距离概念&#xff0c;如欧几里得距离、曼哈顿距离和切比雪夫距离等。这些距离度量直接反映了数据点在多维空间中的位置关系。 欧几里得距离&#xff1a;最为人熟知的几何距…

(四)stm32之通信协议

一.串口通信 1、全双工、半双工、单工 单工:只能一个人传输,只能向一个方向传输 半双工:只能一个人传输,可以多个方向传输 全双工:多方传输,多个方向传输 2、同步通信、一步通信 异步通信:双方时钟可以不同步,发送的信息封装(加上起始位、停止位)实现同步,效率低,…

Arrays:专为数组而生的工具类

创建数组 1.copyOf&#xff1a;复制指定的数组&#xff0c;截取或用 null 填充 String[] intro new String[] { "沉", "默", "王", "二" }; String[] revised Arrays.copyOf(intro, 3); String[] expanded Arrays.copyOf(intro, …

同三维T80004ESL编码器视频使用操作说明书:高清HDMI编码器,高清SDI编码器,4K超清HDMI编码器,双路4K超高清编码器

同三维T80004ESL编码器视频使用操作说明书&#xff1a;高清HDMI编码器&#xff0c;高清SDI编码器&#xff0c;4K超清HDMI编码器&#xff0c;双路4K超高清编码器 同三维T80004ESL编码器视频使用操作说明书&#xff1a;高清HDMI编码器&#xff0c;高清SDI编码器&#xff0c;4K超清…