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. Mysql储存过程6: in / out / inout

    in 为向函数传送进去的值 out 为函数向外返回的值 intout 传送进去的值, 并且还返回这个值 )) begin then select 'true'; else select 'false' ...

  2. 016 sleep,wait,yield,join区别

    1.线程通常有五种状态,创建,就绪,运行.阻塞和死亡状态.2.阻塞的情况又分为三种:(1).等待阻塞:运行的线程执行wait()方法,该线程会释放占用的所有资源,JVM会把该线程放入“等待池”中.进入 ...

  3. Hadoop案例(三)找博客共同好友

    找博客共同好友案例 1)数据准备 以下是博客的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I ...

  4. css让元素不可点击 pointer-events: none;

    张鑫旭大神:http://www.zhangxinxu.com/wordpress/2011/12/css3-pointer-events-none-javascript/ 我们知道form元素里的  ...

  5. javascript右下角弹层及自动隐藏

    在编写项目中总会需要有个右下角弹层提示公告的需求,怎么用更简单方面,更简洁代码,实现更好用户体验这个就是我们的所要做的内容.市场这块弹层很多,但功能不尽如人意.下面分享早些时候自己编写,以及现在还在应 ...

  6. 20169211《Linux内核原理与分析》第四周作业

    20169211<Linux内核原理与分析>第四周作业内容列表 1.教材第3.5章节知识学习总结: 2.实验楼配套实验二实验报告: 1.<linux内核设计与实现>教材第3.5 ...

  7. Django+Nginx+uwsgi搭建自己的博客(五)

    在上一篇博文中,向大家介绍了Users App和Index的前端部分的实现,以及前端与Django的通信部分.至此,我们的博客已经具备一个简单的雏形,可以将其部署在本地的服务器上了.目前较为流行的we ...

  8. 深度学习基础系列(三)| sigmoid、tanh和relu激活函数的直观解释

    常见的激活函数有sigmoid.tanh和relu三种非线性函数,其数学表达式分别为: sigmoid: y = 1/(1 + e-x) tanh: y = (ex - e-x)/(ex + e-x) ...

  9. 磁盘镜像分析工具TSK

    磁盘镜像分析工具TSK   TSK(The Sleuth Kit)是一款基于命令行的数字取证工具集,用于分析磁盘镜像.该工具支持常见的各种文件系统,如Ext2/Ext3/Ext4.Fat/exFat. ...

  10. Ubuntu16.04安装mongodb 及使用

    1.导入软件源的公钥 sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 2.为mongodb创建软件 ...