【算法笔记自学】第 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 电影订票系统小程序 功能介绍 用户 登录 注册 忘记密码 首页 图片轮播 电影信息 电影详情 评论 收藏 预订 电影资讯 资讯详情 用户信息修改 电影评价 我的收藏管理 用户充值 在线客服 我的订单 管理员 登录 个人…

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目录下做…

全渠道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…

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(),…

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

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

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

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

(四)stm32之通信协议

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

【全网最全】2024年辽宁省大学生数学建模竞赛完整思路解析+代码+论文

我是Tina表姐&#xff0c;毕业于中国人民大学&#xff0c;对数学建模的热爱让我在这一领域深耕多年。我的建模思路已经帮助了百余位学习者和参赛者在数学建模的道路上取得了显著的进步和成就。现在&#xff0c;我将这份宝贵的经验和知识凝练成一份全面的解题思路与代码论文集合…

Vue和Element UI 路由跳转,侧边导航的路由跳转,侧边栏拖拽

首先看布局&#xff0c;因为我的用于页面显示的 <router-view> 是通过重定向定位到登陆页的&#xff0c;然后通过登陆页跳转到主页。项目中用到了点击侧边栏的跳转&#xff0c;所以记录下来&#xff0c;方便有需要的人用到~ 阐述 &#xff08;1&#xff09;.content{ di…

26.7 Django单表操作

1. 模型管理器 1.1 Manager管理器 Django ORM中, 每个Django模型(Model)至少有一个管理器, 默认的管理器名称为objects. objects是一个非常重要的管理器(Manager)实例, 它提供了与数据库进行交互的接口.通过管理器, 可以执行数据库查询, 保存对象到数据库等操作.objects管理器…

【Unity学习笔记】第十九 · 物理引擎约束求解解惑(LCP,最优,拉格朗日乘数法,SI,PGS,基于冲量法)

转载请注明出处: https://blog.csdn.net/weixin_44013533/article/details/140309494 作者&#xff1a;CSDN|Ringleader| 在学习物理引擎过程中&#xff0c;有几大问题一直困扰着我&#xff1a; 约束求解到底是LCP还是带约束最优问题&#xff1f;约束求解过程中拉格朗日乘数法…

SpringBoot整合JWT示例教程

1. JWT简介 JSON Web Token (JWT) 是一种开放标准&#xff08;RFC 7519&#xff09;&#xff0c;它定义了一种紧凑且自包含的方式&#xff0c;用于在各方之间作为 JSON 对象安全地传输信息。由于这些信息是经过数字签名的&#xff0c;因此可以被验证和信任。JWT 通常用于身份验…

openharmony上传图片,并获取返回路径

适用条件&#xff1a; openharmony开发 4.0 release版本&#xff0c;对应能力API10 一直不断尝试&#xff0c;一会用官方提供的上传文件&#xff0c;一会用第三方库的axios都不行&#xff0c; 一会报错‘没权限&#xff0c;一会报错’路径错误&#xff0c;还有报错‘401参数错…

本地部署,去除动漫图像背景Anime Remove Background

目录 摘要 引言 深度学习在动漫角色中的应用 1.​U-Net 2.Mask R-CNN 3.ISNet 模型 4.MODNet 模型 5.InSPyReNet 模型 本地部署 运行效果 测验结果​ Tip&#xff1a; 摘要 动漫图像背景去除是一项在图像处理和计算机视觉领域具有重要应用的技术&#xff0c;广泛应用于…