Question 191.2022 Winter Holiday Ladder Competition Training-7-10 Judgment about Heap (25 points)


Question 191.2022 Winter Holiday Ladder Competition Training-7-10 Judgment about Heap (25 points)


1. The topic

insert image description here

2. Problem solving

There are two points to pay attention to in this question. The first is to determine which question the input is asking, and the other is to build a heap. The former feels more difficult to handle, but when inputting, it is no problem to grasp the different points of each problem to judge and improve the characteristics of the input. The latter is to build the heap directly. You can choose stl to build the heap or directly insert the heap, and the code is given. As for how to judge the correctness of those problems, it is nothing more than to see whether the position of the node in the array satisfies the correct relationship (q1:x is the root node, that is, smallheap[1]==x q2: The position of the two parent nodes in the corresponding array is the same q3: y's position in the parent node array is equal to x's position in the array q4: x's position in the parent node array is equal to y's position in the array). code show as below:

#include <bits/stdc++.h>

using namespace std;

int N,M;
const int Inf=0x3f3f3f3f;
vector<int> smallHeap;
string judge;//用于问题的输入,可以占着无用串,也可以作为关键串参与判断

int getPos(int query)//找节点所处的数组位置
{
    
    
    for(int pos=1; pos<=N; pos++)
    {
    
    
        if(query==smallHeap[pos])
        {
    
    
            return pos;
        }
    }
    return -1;
}

int main()
{
    
    
    cin>>N>>M;
    smallHeap.push_back(-Inf);//建堆法子1--STL建堆
    for(int i=0; i<N; i++)
    {
    
    
        int data;
        scanf("%d",&data);
        smallHeap.push_back(data);
        make_heap(++smallHeap.begin(),smallHeap.end(),greater<int>());
    }
    /*
    for(int i=1; i<=N; ++i)//建堆法子2--插入建堆
    {
        int data;
        scanf("%d",&data);
        smallHeap.push_back(data);
        int k=i;
        while(k>1&&smallHeap[k]<smallHeap[k/2])
        {
            swap(smallHeap[k],smallHeap[k/2]);
            k=k/2;
        }
    }
    */
    for(int i=0; i<M; i++)
    {
    
    
        int x,y;
        scanf("%d",&x);
        cin>>judge;
        if(judge=="and")//q2
        {
    
    
            scanf("%d",&y);
            cin>>judge;
            cin>>judge;
            int fpos1=getPos(x)/2;
            int fpos2=getPos(y)/2;
            if(fpos1==fpos2)
            {
    
    
                printf("T\n");
            }
            else
            {
    
    
                printf("F\n");
            }
        }
        else
        {
    
    
            cin>>judge;
            if(judge=="a")//q4
            {
    
    
                cin>>judge;
                cin>>judge;
                scanf("%d",&y);
                int pos1=getPos(x);
                int pos2=getPos(y);
                if(pos2==pos1/2)//你知道吗,我这里当时只写了一个=,然后测试点3错了,之后我找了一个半小时才找到为什么错,我真想xs自己
                {
    
    
                    printf("T\n");
                }
                else
                {
    
    
                    printf("F\n");
                }
            }
            else
            {
    
    
                cin>>judge;
                if(judge=="root")//q1
                {
    
    
                    if(smallHeap[1]==x)
                    {
    
    
                        printf("T\n");
                    }
                    else
                    {
    
    
                        printf("F\n");
                    }
                }
                else//q3
                {
    
    
                    cin>>judge;
                    scanf("%d",&y);
                    int pos1=getPos(x);
                    int pos2=getPos(y);
                    if(pos1==pos2/2)
                    {
    
    
                        printf("T\n");
                    }
                    else
                    {
    
    
                        printf("F\n");
                    }
                }
            }
        }
    }
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324346871&siteId=291194637