7-11 关于堆的判断 (25 分) (建堆)

 

 

 

 要学会建堆,然后知道用数组模拟堆的优点。

复制代码
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
const int N = 1010;
int heap[N];
int n, m;

void insert(int pos, int x)  // 建堆
{
    heap[pos] = x;
    while(pos > 1) {
        if(heap[pos] < heap[pos >> 1]) {
            swap(heap[pos], heap[pos >> 1]);
            pos >>= 1;
        } else {
            break;
        }
    }
}
int main()
{
    cin >> n >> m;
    map<int,int>p;  // 用map 来 记录每个节点对应在数组中的下标
    for(int i = 1; i <= n; i++)
    {
        int x;
        cin >> x;
        insert(i, x);
    }
    
    for(int i = 1; i <= n; i++) p[heap[i]] = i;
    
    while(m--)
    {
        string s;
        int x, y, flag = 0;
        cin >> x >> s;
        if(s == "and") {                         // 根据每句话的特性来判断。 我觉得这非常妙
            cin >> y >> s >> s;
            if(heap[p[x] ^ 1] == y) flag = 1;
        } else {
            cin >> s;
            if(s == "a") {
                cin >> s >> s >> y;
                if(heap[p[x] >> 1] == y) flag = 1;
            } else {
                cin >> s;
                if(s == "root") {
                    if(heap[1] == x) flag = 1;
                } else {
                    cin >> s >> y;
                    if(heap[p[y] >> 1] == x) flag = 1;
                }
            }
        }
        if(flag) cout << "T" << endl;
        else cout << "F" << endl;
    }
    return 0;
}
复制代码

 

posted @   飘向远方丶  阅读(83)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性
· 2025年我用 Compose 写了一个 Todo App
点击右上角即可分享
微信分享提示