涉及:建小顶堆 (以数组为本,序号从1开始,依据heap[t]<heap[t/2])
堆中数据关系的判断(问题所在:容易多打字符串)
#include<bits/stdc++.h>
using namespace std;
int heap[1010],n;
int find_f(int x)
{
for(int i=1;i<=n;i++)
if(heap[i]==x)
return i;
return -1;
}
int main()
{
string tmp;
int m;
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>heap[i];
int t=i;
while(t>1&&heap[t]<=heap[t/2])
{
swap(heap[t],heap[t/2]);
t/=2;
}
}
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>tmp;
if(tmp=="is")
{
cin>>tmp;
if(tmp=="the")
{
cin>>tmp;
if(tmp=="root")
{
cout<<((heap[1]==x)?"T":"F")<<endl;
}
else
{
cin>>tmp>>y;
cout<<(heap[find_f(y)/2]==x?"T":"F")<<endl;
}
}
else
{
cin>>tmp>>tmp>>y;
cout<<(heap[find_f(x)/2]==y?"T":"F")<<endl;
}
}
else
{
cin>>y;
getchar();
getline(cin,tmp);
int x1=find_f(x);
int x2=find_f(y);
cout<<(x1/2==x2/2?"T":"F")<<endl;
}
}
return 0;
}