L2-012 关于堆的判断 (25 分)
2019-03-10 本文已影响0人
Mr_Vetr
这一题真的无语,如果用线性调整heap的话是不对的,题目要求是一个一个插入一个空的MinHeap,所以只能插入。
#include<bits/stdc++.h>
using namespace std;
const int N = 1010;
int size;
int n;
int h[N];
int p[N];
void Predown(int index){
int x = p[index];
int child,parent;
for(parent = index;parent *2 <=size; parent = child){
child = parent *2;
if( child != size && p[child+1] < p[child]){
child++;
}
if( x <= p[child])
break;
else
p[parent] = p[child];
}
p[parent] = x;
}
void insertH(int x){
int i;
for( i = ++size; p[i/2] > x; i/=2){
p[i] = p[i/2];
}
p[i] = x;
}
int main()
{
string s;
int word;
int m;
cin>>n>>m;
p[0] = INT_MIN;
for(int i=1; i<=n; ++i)
cin>>h[i];
size = 0;
for(int i=1; i<=n; ++i){
insertH(h[i]);
}
for(int i=0; i<m; ++i){
cin>>word>>s;
if(s == "and"){
int word2;
cin>>word2;
getline(cin,s);
int pos1 = find(p,p+size+1,word) - p;
int pos2 = find(p,p+size+1,word2) - p;
if((pos1 == pos2+1||pos2==pos1+1)&& pos1/2 == pos2/2)
cout<<"T"<<endl;
else
cout<<"F"<<endl;
}else{
cin>>s;
if( s == "a"){
int father;
cin>>s;
cin>>s;
cin>>father;
int pos = find(p,p+size+1,father)-p;
int pos2 = find(p,p+size+1,word)-p;
if( pos == pos2/2)
cout<<"T"<<endl;
else
cout<<"F"<<endl;
}else{
cin>>s;
if(s == "root"){
if(p[1] == word)
cout<<"T"<<endl;
else
cout<<"F"<<endl;
}
else{
cin>>s;
int child;
cin>>child;
int pos = find(p,p+size+1,word)-p;
int pos2 = find(p,p+size+1,child)-p;
if( pos2/2 == pos )
cout<<"T"<<endl;
else
cout<<"F"<<endl;
}
}
}
}
return 0;
}