二叉树

  • P4715淘汰赛

本题一眼就是二叉树,不过是满二叉树,把左右节点做比较,大的就做父节点,类似线段树的打法
不过这题也可以用队列+结构体实现,比对时大的继续留在队列,小的除去,符合FIFO原则,最后
专门去取最后俩个做对比就好了

点击查看代码
#include<bits/stdc++.h>
using namespace std;
int n;
struct node{
	int id,w;
};
queue <node> q;
int main(){
	scanf("%d",&n);
	n=1<<n;
	for(int i=1;i<=n;++i){
		int w;
		cin>>w;
		q.push(node{i,w});
	}
	while(q.size()>2){
		node a,b;
		a=q.front();
		q.pop();
		b=q.front();
		q.pop();
		if(a.w>b.w) q.push(a);
		else q.push(b);
	}
	node x,y;
	x=q.front();
	q.pop();
	y=q.front();
	q.pop();
	if(x.w<y.w) cout<<x.id<<endl;
	else cout<<y.id<<endl;
	return 0;
}
posted @ 2022-10-09 21:47  啊梓  阅读(15)  评论(0编辑  收藏  举报