L2-012 关于堆的判断 (25 分)

这一题真的无语,如果用线性调整heap的话是不对的,题目要求是一个一个插入一个空的MinHeap,所以只能插入。

#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int size;
int n;
int h[N];
int p[N];
void Predown(int index){
    int x = p[index];
    int child,parent;
    for(parent = index;parent *2 <=size; parent = child){
        child = parent *2;
        if( child != size && p[child+1] < p[child]){
            child++;
        }
        if( x <= p[child])
            break;
        else
            p[parent] = p[child];
    }
    p[parent] = x;
}
void insertH(int x){
    int i;
    for( i = ++size; p[i/2] > x; i/=2){
        p[i] = p[i/2];
    }
    p[i] = x;
}
int main()
{
    string s;
    int word;
    int m;
    cin>>n>>m;
    p[0] = INT_MIN;
    for(int i=1; i<=n; ++i)
        cin>>h[i];
    size = 0;
    for(int i=1; i<=n; ++i){
        insertH(h[i]);
    }
    for(int i=0; i<m; ++i){
        cin>>word>>s;
        if(s == "and"){
            int word2;
            cin>>word2;
            getline(cin,s);
            int pos1 = find(p,p+size+1,word) -  p;
            int pos2 = find(p,p+size+1,word2) - p;
            if((pos1 == pos2+1||pos2==pos1+1)&& pos1/2 == pos2/2)
                cout<<"T"<<endl;
            else
                cout<<"F"<<endl;
        }else{
            cin>>s;
            if( s == "a"){
                int father;
                cin>>s;
                cin>>s;
                cin>>father;
                int pos = find(p,p+size+1,father)-p;
                int pos2 = find(p,p+size+1,word)-p;
                if( pos == pos2/2)
                    cout<<"T"<<endl;
                else
                    cout<<"F"<<endl;
            }else{
                cin>>s;
                if(s == "root"){
                    if(p[1] == word)
                        cout<<"T"<<endl;
                    else
                        cout<<"F"<<endl;
                }
                else{
                    cin>>s;
                    int child;
                    cin>>child;
                    int pos = find(p,p+size+1,word)-p;
                    int pos2 = find(p,p+size+1,child)-p;
                    if( pos2/2 == pos )
                        cout<<"T"<<endl;
                    else
                        cout<<"F"<<endl;
                }
            }
        }
    }
    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 怀孕时候,经常出差,周末都加班~ 晚上还追剧,我记得看完了整个《权利的游戏》。 后面,这一切导致身体没有足够精力分...
    琳猫411阅读 1,486评论 0 0
  • (一) 这两天被网上那个“我为什么讨厌生孩子”的文章刷屏了。 身为母亲的题主,以一种极端潇洒的姿态,展示了她“只生...
    蜜丝赵阅读 5,673评论 16 19
  • 在开始今天的演讲之前,我想问大家一个问题,你们觉得成人之后大脑还会不会发育…… 我们以前的观点是在一个人成年之后他...
    姬如晓亦阅读 1,666评论 0 0
  • 自信这一点实在是太重要了。相信自己,一步步来做,不要总盯着自己做不了的事,因为你不可能一步登天。 团队的分歧 今天...
    三桶火火阅读 2,361评论 0 0

友情链接更多精彩内容