7-3 stack path (25 minutes)

Original link: http://www.cnblogs.com/sykline/p/10579014.html

topic:  

The insertion of a series of a given number of minor vertex initially empty stack H[]. Then for any given index i, from the print H[i]path to the root node.

Ideas:

From the current forward index start (top of the stack) are compared, if the upper layer is greater than the value of the current value, the value will be moved to the current position of the layer, so far not known until the mobile.

Code:

#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <iomanip>
#define MAX 1000000000
#define inf 0x3f3f3f3f
#define FRE() freopen("in.txt","r",stdin)

using namespace std;
typedef long long ll;
const int maxn = 10005;
int heap[maxn];
int n,m;

void insertHeap(int X, int Level) 
{ 
    the while (Level / 2 > 0 && heap [Level / 2 ]> X) // if the upper layer is greater than the current value of 
    { 
        heap [Level] = heap [Level / 2 ]; / / the downward movement of the upper hierarchy value 
        Level / = 2 ; 
    } 
    heap [Level] = X; // find the location of the discharge current value of the 
    return ; 
} 

void showPath ( int Level) 
{ 
    the printf ( " % D " , heap [Level]); 
    Level / = 2;
    while(level>0)
    {
        printf(" %d",heap[level]);
        level/=2;
    }
    printf("\n");
}

int main()
{
    int x;
    scanf("%d%d",&n,&m);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",&x);
        insertHeap(x,i);
    }
    for(int i=0; i<m; i++)
    {
        scanf("%d",&x);
        showPath(x);
    }
    return 0;
}

 

Reproduced in: https: //www.cnblogs.com/sykline/p/10579014.html

Guess you like

Origin blog.csdn.net/weixin_30319097/article/details/94789425