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

题目链接

思路

小根堆的建立+字符串处理,真的好爱考字符串呀。。。

代码

#include<bits/stdc++.h>


using namespace std;
const int N  = 1e3+9;
int a[N];
unordered_map<int,int> pos;
//堆化 
void build(int n){
	int t=n;
	while(t/2>=1 && a[t/2]>a[t]){
		swap(a[t/2],a[t]);
		t/=2;
	}
}
int main(){
	int n,m;
	scanf("%d %d",&n,&m);
	for(int i=1; i<=n; i++){
		scanf("%d",a+i);
		//在堆中插入 
		build(i);
	}
	for(int i=1; i<=n; i++) pos[a[i]]=i;
	while(m--){
		string s;
		int x=0,y=0;
		scanf("%d",&x);
		while(cin>>s)
		{
			if(s=="and") {
				scanf("%d",&y);
			}
			if(s=="root"){
				if(pos[x]==1) puts("T");
				else puts("F");
				break;
			}
			else if(s=="siblings"){
				if(pos[x]/2==pos[y]/2) puts("T");
				else puts("F");
				break;
			}
			else if(s=="parent"){
				cin>>s;
				scanf("%d",&y);
				if(pos[x]==pos[y]/2) puts("T");
				else puts("F");
				break;
			}
			else if(s=="child"){
				cin>>s;
			    scanf("%d",&y);
				if(pos[x]/2==pos[y]) puts("T");
				else puts("F");
				break;
			}
		}
	
	}
	return 0;
}
posted @   翔村亲亲鸟  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示