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

就是一个最小根堆。

最小根堆的性质,根节点小于等于子树的完全二叉树吧。

构建最小根堆的过程就是一个自下向上的过程。

#include<iostream>
#include<string>
#include<map>
using namespace std;

const int maxn = 10010;
const int inf = 999999;
int a[maxn], cnt;
void creat(int x){
    a[++cnt] = x;
    int t = cnt;
    while (t > 1 && (a[t / 2] > a[t])){
        swap(a[t], a[t / 2]);    t /= 2;
    }
    a[t] = x;
}

int n, m, x, y;
string s;
map<int, int>p;

int main(){
    cin >> n >> m;
    for (int i = 1; i <= n; ++i){ cin >> x; creat(x); }
    for (int i = 1; i <= n; ++i){ p[a[i]] = i; }
    for (int i = 0; i < m; ++i){
        cin >> x;
        cin >> s;
        if (s[0] == 'a'){
            cin >> y >> s >> s;
            if (p[x] / 2 == p[y] / 2)cout << "T" << endl;
            else cout << "F" << endl;
        }
        else{
            cin >> s;
            cin >> s;
            if (s[0] == 'r')
            { if (p[x] == 1)cout << "T" << endl; else cout << "F" << endl; }
            else if (s[0] == 'p')
            {
                cin >> s; cin >> y; if (p[x] == p[y] / 2)cout << "T" << endl; else cout << "F" << endl;
            }
            else if (s[0] == 'c')
            {
                cin >> s; cin >> y; if (p[x] / 2 == p[y])cout << "T" << endl; else cout << "F" << endl;
            }
        }
    }
}

 

上一篇:L2-012 关于堆的判断


下一篇:012 Ceph多区域网关