04-树9. Path in a Heap (25)
来源:互联网 发布:手机淘宝4.0 编辑:程序博客网 时间:2024/06/05 19:16
04-树9. Path in a Heap (25)
Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index i, you are supposed to print the path from H[i] to the root.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N and M (<=1000) which are the size of the input sequence, and the number of indices to be checked, respectively. Given in the next line are the N integers in [-10000, 10000] which are supposed to be inserted into an initially empty min-heap. Finally in the last line, M indices are given.
Output Specification:
For each index i in the input, print in one line the numbers visited along the path from H[i] to the root of the heap. The numbers are separated by a space, and there must be no extra space at the end of the line.
Sample Input:5 346 23 26 24 105 4 3Sample Output:
24 23 1046 23 1026 10
#include <stdio.h>void insert(int *heap, int val) {int child = ++heap[0];int parent = child / 2;while (parent > 0 && heap[parent] > val) {//上滤heap[child] = heap[parent];child = parent;parent = child / 2;}heap[child] = val;}int main() {//freopen("test.txt", "r", stdin);int n, m;scanf("%d%d", &n, &m);int heap[1000] = {};//0位置放置堆中元素大小,1位置为堆顶for (int i = 0; i < n; ++i) {int num;scanf("%d", &num);insert(heap, num);}while (m--) {int index;scanf("%d", &index);while (index > 0) {printf("%d", heap[index]);if (index != 1)printf(" ");index /= 2;}printf("\n");}return 0;}
题目链接:http://www.patest.cn/contests/mooc-ds/04-%E6%A0%919
- 04-树9. Path in a Heap (25)
- PAT 数据结构 04-树9. Path in a Heap (25)
- 04-树9. Path in a Heap (25)
- 04-树9. Path in a Heap (25) -- 小堆树-利用其性质
- 05-树6. Path in a Heap (25)
- 05-树6. Path in a Heap (25)
- 05-树6. Path in a Heap
- Dijkstra+Heap-HDU-4725-The Shortest Path in Nya Graph
- SPOJ 1437 Longest path in a tree
- leetcode Longest Increasing Path in a Matrix
- 329. Longest Increasing Path in a Matrix
- [LeetCode] Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix
- leetcode: Longest Increasing Path in a Matrix
- Leetcode: Longest Increasing Path in a Matrix
- Longest Increasing Path in a Matrix
- leetcode:Longest Increasing Path in a Matrix
- [LeetCode]Longest Increasing Path in a Matrix
- Codeforces Round #260 (Div. 2) E. Civilization
- 第二章 类型、运算符、表达式
- HDU 4771 Stealing Harry Potter's Precious (深搜+广搜)
- [Leetcode 150, Medium] Evaluate Reverse Polish Notation
- Error 1310 Error writing to file:C:\Windows\...*.dll. Verify that you have access to that directory
- 04-树9. Path in a Heap (25)
- .net学习之委托
- ThinkPHP 空操作处理方法
- nyoj-20-吝啬的国度(深搜)
- Java错误提示------------------Cannot reduce the visibility of the inherited method from MouseListener
- .net学习之messagebox的各种枚举
- pcl配置中遇到的问题
- JQuery基础attribute
- html 代码初识——<a>标签