其他分享
首页 > 其他分享> > L2-012 关于堆的判断 (25 分)

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;
            }
        }
    }
}

 

标签:25,cnt,cout,int,cin,根堆,012,L2,include
来源: https://www.cnblogs.com/ALINGMAOMAO/p/10604360.html