萌新求助RE7个点
查看原帖
萌新求助RE7个点
316855
Gunpowder_OI楼主2020/10/2 19:17
#include <iostream>
#include <vector>
using namespace std;
vector<int> v;
int getfather (int n){
    if (n == 0)return n;
    else return n / 2;
}
int getsmallestchildrenplace (int n){
    if ((n + 1) * 2 - 1 == v.size ())return (n + 1) * 2 - 2;
    if ((n + 1) * 2 - 2 == v.size ())return n;
    if (v[(n + 1) * 2 - 1] > v[(n + 1) * 2])return (n + 1) * 2;
    return (n + 1) * 2 - 1;
}
int getsmallestchildren (int n){
    return v[getsmallestchildrenplace (n)];
}
void add (int n){
    v.push_back (n);
    int nplace = v.size () - 1;
    while (n < v[getfather (nplace)]){
        swap (v[nplace], v[getfather (nplace)]);
        nplace = getfather (nplace);
    }
}
int getsmallest (){
    return v[0];
}
void deletesmallest (){
    v[0] = v.back ();
    v.pop_back ();
    int frontplace = 0;
    while (v[frontplace] > getsmallestchildren (frontplace)){
        swap (v[frontplace], v[getsmallestchildrenplace (frontplace)]);
        frontplace = getsmallestchildrenplace (frontplace);
    }
}
int main (){
    int n, a, tmp;
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> a;
        if (a == 1){
            cin >> tmp;
            add (tmp);
        }
        if (a == 2)cout << getsmallest () << endl;
        if (a == 3)deletesmallest ();
    }
    return 0;
}

评测记录

急,在线等
2020/10/2 19:17
加载中...