05-树6. Path in a Heap (25)

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://www.patest.cn/contests/mooc-ds2015spring/05-%E6%A0%916

Description

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

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

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 3 46 23 26 24 10 5 4 3

Sample Output

24 23 10 46 23 10 26 10

HINT

题意

题解:

随便拿个数组模拟一下小根堆就好了……

代码:

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
/* inline void P(int x)
{
Num=0;if(!x){putchar('0');puts("");return;}
while(x>0)CH[++Num]=x%10,x/=10;
while(Num)putchar(CH[Num--]+48);
puts("");
}
*/
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//**************************************************************************************
int a[maxn];
int main()
{
//test;
int n=read(),m=read();
for(int i=;i<=n;i++)
{
a[i]=read();
int tmp=i;
while(tmp!=&&a[tmp]<a[tmp/])
{
swap(a[tmp],a[tmp/]);
tmp/=;
}
}
for(int i=;i<=m;i++)
{
int first=;
int x=read();
while(x!=)
{
if(first)
printf("%d",a[x]),first=;
else
printf(" %d",a[x]);
x/=;
}
printf("\n");
}
}

05-树6. Path in a Heap (25) 小根堆的更多相关文章

  1. pat04-树9. Path in a Heap (25)

    04-树9. Path in a Heap (25) 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue Insert ...

  2. Java内存泄漏分析系列之六:JVM Heap Dump(堆转储文件)的生成和MAT的使用

    原文地址:http://www.javatang.com JVM Heap Dump(堆转储文件)的生成 正如Thread Dump文件记录了当时JVM中线程运行的情况一样,Heap Dump记录了J ...

  3. 小根堆(Heap)的详细实现

    堆的介绍 Heap是一种数据结构具有以下的特点: 1)完全二叉树 2)heap中存储的值是偏序 Min-heap: 父节点的值小于或等于子节点的值 Max-heap: 父节点的值大于或等于子节点的值 ...

  4. Binary Heap(二叉堆) - 堆排序

    这篇的主题主要是Heapsort(堆排序),下一篇ADT数据结构随笔再谈谈 - 优先队列(堆). 首先,我们先来了解一点与堆相关的东西.堆可以实现优先队列(Priority Queue),看到队列,我 ...

  5. Java堆内存Heap与非堆内存Non-Heap

    堆(Heap)和非堆(Non-heap)内存     按照官方的说法:“Java 虚拟机具有一个堆,堆是运行时数据区域,所有类实例和数组的内存均从此处分配.堆是在 Java 虚拟机启动时创建的.”“在 ...

  6. PAT 05-树6 Path in a Heap

    这次的作业完全是依葫芦画瓢,参照云课堂<数据结构>(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师 ...

  7. PAT005 Path in a Heap

    题目: Insert a sequence of given numbers into an initially empty min-heap H. Then for any given index ...

  8. 笛卡尔树 POJ ——1785 Binary Search Heap Construction

    相应POJ 题目:点击打开链接 Binary Search Heap Construction Time Limit: 2000MS   Memory Limit: 30000K Total Subm ...

  9. LeetCode之“树”:Path Sum && Path Sum II

    Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path suc ...

随机推荐

  1. Linux 添加普通用户到 sudoers 文件

    前言 Linux 的普通用户(uid >= 500)不具有某些命令的执行权限,为了执行较高权限的命令,一般有两种方法: 第一种是使用 su - 命令切换到 root 用户去执行: 另外一种方法是 ...

  2. linux下的僵尸进程处理SIGCHLD信号【转】

    转自:http://www.cnblogs.com/wuchanming/p/4020463.html 什么是僵尸进程? 首先内核会释放终止进程(调用了exit系统调用)所使用的所有存储区,关闭所有打 ...

  3. python基础===基于cv2的播放器

    import cv2 import threading import win32gui,win32con class Producer(threading.Thread): ""& ...

  4. 在Ubuntu上安装Redis MySQL MongoDB memcached Nginx

    1.安装Redis sudo apt-get install redis-server 2.安装MySQL sudo apt-get install mysql-server 3.安装MongoDB ...

  5. mac上卸载mysql

    在终端输入一下命令 sudo rm /usr/local/mysqlsudo rm -rf /usr/local/mysql*sudo rm -rf /Library/StartupItems/MyS ...

  6. ifconfig与内核通信 ifreq 结构体分析和使用

    结构原型: /* * Interface request structure used for socket * ioctl's.  All interface ioctl's must have p ...

  7. oracle 一个网站

    http://www.oracle.com/technetwork/cn/articles/11g-pivot-101924-zhs.html

  8. 深度学习方法(九):自然语言处理中的Attention Model注意力模型

    欢迎转载,转载请注明:本文出自Bin的专栏blog.csdn.NET/xbinworld. 技术交流QQ群:433250724,欢迎对算法.技术感兴趣的同学加入. 上一篇博文深度学习方法(八):Enc ...

  9. 【Mac】【已解决】连接Android机器提示“此电脑不能读取您插入的磁盘”

    出现的报错提示页面截图如下: 解决方案: 下载“Android File Transfer.dmg”安装在Mac. 打开USB调试,连接手机即可读取手机磁盘.   下载链接:https://www.t ...

  10. 791. Custom Sort String

    S and T are strings composed of lowercase letters. In S, no letter occurs more than once. S was sort ...