堆中的路径

发布时间:2019年10月03日 阅读:305 次

STL


#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int a[999999]={0};
vector<int>heap;
int Find(int x)
{
    return distance(heap.begin(),find(heap.begin(),heap.end(),x));
}
int main()
{

    int n,m;
    cin>>n>>m;
    int tmp;
    for(int i=1;i<=n;i++)
    {
        cin>>tmp;
        heap.push_back(tmp);
        make_heap(heap.begin(),heap.end(),greater<int>());
    }
    for(int j=1;j<=m;j++)
    {
        cin>>tmp;
        int top = 1;
        while(1)
        {
            if(top)top = 0;
            else printf(" ");
            printf("%d",heap[tmp-1]);
            tmp = (tmp)/2;
            if(tmp==0)break;
        }
        printf("\n");
    }
    return 0;
}


Tag:
相关文章

发表评论: