zoukankan      html  css  js  c++  java
  • L2-012. 关于堆的判断

    将一系列给定数字顺序插入一个初始为空的小顶堆H[]。随后判断一系列相关命题是否为真。命题分下列几种:

    • “x is the root”:x是根结点;
    • “x and y are siblings”:x和y是兄弟结点;
    • “x is the parent of y”:x是y的父结点;
    • “x is a child of y”:x是y的一个子结点。

    输入格式:

    每组测试第1行包含2个正整数N(<= 1000)和M(<= 20),分别是插入元素的个数、以及需要判断的命题数。下一行给出区间[-10000, 10000]内的N个要被插入一个初始为空的小顶堆的整数。之后M行,每行给出一个命题。题目保证命题中的结点键值都是存在的。

    输出格式:

    对输入的每个命题,如果其为真,则在一行中输出“T”,否则输出“F”。

    输入样例:

    5 4
    46 23 26 24 10
    24 is the root
    26 and 23 are siblings
    46 is the parent of 23
    23 is a child of 10
    

    输出样例:

    F
    T
    F
    T
    #include<algorithm>
    #include<iostream>
    #include<cstdio>
    #include<vector>
    #include<set>
    #include<queue>
    #include<cmath>
    #include<map>
    #include<cstring>
    #include<string>
    using namespace std;
    
    const int N = 4000 + 5;
    
    int Heap[N], n, m;
    void AdjustDown(int root, int n){
        int child = root * 2 + 1;
    
        while(child < n){
            if(child + 1 < n && Heap[child] > Heap[child + 1]) child++;
            if(Heap[child] >= Heap[root]) break;
            swap(Heap[root], Heap[child]);
            root = child;
            child = root * 2 + 1;
        }
    }
    
    void MakeHeap(int n){
        for(int i = n / 2 - 1; i >= 0; i--)
            AdjustDown(i, n);
    }
    char s1[100], s2[100], s3[100];
    map<int, int> mat;
    void Solve(){
        int x, y;
        scanf("%d %s",&x, s1);
        if(s1[0] =='a'){
            scanf("%d %s %s", &y, s1, s1);
            printf("%c
    ", ((mat[x]- 1)/ 2 != (mat[y] - 1)/2)?'F':'T');
        }else{
            int cur = 2;
            while(cur--)scanf("%s", s1);
            if(s1[0] == 'r'){
                printf("%c
    ", Heap[0] != x?'F':'T');
            }else if(s1[0] == 'p'){
                scanf("%s%d", s1, &y);
                printf("%c
    ", (mat[y]-1)/2 == mat[x]?'T':'F');
            }else{
                scanf("%s %d", s1, &y);
                printf("%c
    ", (mat[x] - 1)/2 == mat[y]?'T':'F');
            }
        }
    }
    
    int main(){
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; i++){
            scanf("%d", &Heap[i]);
            MakeHeap(i + 1);
        }
        //MakeHeap();
        for(int i = 0; i < n; i++)
            mat[Heap[i]] = i;
        while(m --) Solve();
    }
  • 相关阅读:
    ## 序列化和反序列化
    C#小型资源管理器
    codeforces #310 div1 B
    codeforces #310 div1 A
    BZOJ 1030 文本生成器
    BZOJ 2806 cheat
    BZOJ 2553 禁忌
    BZOJ 4199 品酒大会
    codeforces #309 div1 D
    codeforces #309 div1 C
  • 原文地址:https://www.cnblogs.com/Pretty9/p/8625484.html
Copyright © 2011-2022 走看看