Path in a Heap (25)

被几道30分的题困扰好久 = = 果然自己还是太弱了

这道25分的还是比较简单

做做就做出来了

#include <iostream>
#include <cstring>
using namespace std;
long long i;
long long *A;
void insert(long long a);
int main()
{
    long long n, m;
    cin >> n >> m;

    A = (long long *)malloc((n + 1)*sizeof(long long));

    for (i = 1; i <= n; i++){
        long long a;
        cin >> a;
        insert(a);
    }

    for (i = 0; i < m; i++){
        long long q;
        cin >> q;
        cout << A[q];
        q /= 2;
        while (q>0){
            cout << " " << A[q];
            q /= 2;
        }

        cout << endl;
    }
}
void insert(long long a)
{
    long long m = i;
    A[m] = a;
    while ( m/2 > 0){   //循环条件一开始写成了m>0,对计算机来说 = = 真是不一样啊
        if (A[m] < A[m / 2]){
            A[m] ^= A[m / 2];
            A[m / 2] ^= A[m];
            A[m] ^= A[m / 2];
        }
        m /= 2;
    }
}
时间: 2024-08-03 17:51:36

Path in a Heap (25)的相关文章

04-树9. Path in a Heap (25)

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

PAT Path in a Heap

Path in a Heap 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. 堆的建立基础操作 非常简单 建议看看mooc何老师的视频 就明白了 这里就不再赘述了 只是我的代码不知道为何 有两个节点时间超限  希望大家能帮忙看看 1

PAT 05-树6 Path in a Heap

这次的作业完全是依葫芦画瓢,参照云课堂<数据结构>(http://mooc.study.163.com/learn/ZJU-1000033001#/learn/content)中何钦铭老师课件中有关建堆及插入的内容,再加上自己写的一个矬函数(竟然传了4个参数),OK了!题设要求及代码实现如下 1 /* 2 Name: 3 Copyright: 4 Author: 5 Date: 05/04/15 19:34 6 Description: 7 Insert a sequence of given

PAT005 Path in a Heap

题目: 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 lin

PAT树_层序遍历叶节点、中序建树后序输出、AVL树的根、二叉树路径存在性判定、奇妙的完全二叉搜索树、最小堆路径、文件路由

<pre class="code"><span style="font-family: %value; font-size: 14px;">03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each inpu

Longest Absolute File Path -- LeetCode

Suppose we abstract our file system by a string in the following manner: The string "dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext" represents: dir subdir1 subdir2 file.ext The directory dir contains an empty sub-directory subdir1 and a sub-directory 

JVM Heap认知

一.JVM Heap分为三部分:新生代.老年代.永久代: 新生代:用于存放JVM新分配的java对象: 老年代:新生代中经过垃圾回收没有回收掉的对象将被copy到老年代: 永久代:存放Class.Method元信息,也就是反射对象,一般设置为128M足够,设置原则是预留30%空间. 二.GC的引发: 1.新生代拥有2个线程: (1)当新生代的Eden代满了引发普通GC,仅仅回收新生代: (2)新生代空间不足时,会把存活的对象转移到老生代. 2.老年代:当老年代满时引发Full GC,它将会同时回

golang gc 优化思路以及实例分析

一个即将上线的go 写的高频服务,压测的时候发现 gc 特别高,高到10%-15% 左右了,本文记录下优化 gc 的过程和和思路.线上环境1.10. 首先,查看gc 是否有异常,我们可以使用 gctrace 跟踪实时的gc .执行下面命令可以看到gc 的实时信息. GODEBUG=gctrace=1 go run cmd/agent_bin.go 输出结果如下: gc 45 @37.801s 11%: 0.19+627+0.29 ms clock, 0.38+424/621/0+0.59 ms

数据挖掘:基于Spark+HanLP实现影视评论关键词抽取(1)

1. 背景 近日项目要求基于爬取的影视评论信息,抽取影视的关键字信息.考虑到影视评论数据量较大,因此采用Spark处理框架.关键词提取的处理主要包含分词+算法抽取两部分.目前分词工具包较为主流的,包括哈工大的LTP以及HanLP,而关键词的抽取算法较多,包括TF-IDF.TextRank.互信息等.本次任务主要基于LTP.HanLP.Ac双数组进行分词,采用TextRank.互信息以及TF-IDF结合的方式进行关键词抽取. 说明:本项目刚开始接触,因此效果层面需迭代调优. 2. 技术选型 (1)