poj 3784 Running Median/sbt

原题链接:http://poj.org/problem?id=3784

动态地求中位数,套平衡树做就行了。。。

代码如下:

#include<iostream>
#include<algorithm>
const int Max_N = 10000;
struct SBT *null;
struct SBT{
	int val, size;
	struct SBT *ch[2];
	inline void push_up(){
		size = ch[0]->size + ch[1]->size + 1;
	}
}stack[Max_N << 2], *root;
int sz = 0;
void init(){
	null = &stack[sz++];
	null->val = -1, null->size = 0;
	root = null;
}
void rotate(SBT* &x, int d){
	SBT *k = x->ch[!d];
	x->ch[!d] = k->ch[d];
	k->ch[d] = x;
	k->size = x->size;
	x->push_up();
	x = k;
}
void Maintain(SBT* &x, int d){
	if (x->ch[d] == null) return;
	if (x->ch[d]->ch[d]->size > x->ch[!d]->size){
		rotate(x, !d);
	} else if (x->ch[d]->ch[!d]->size > x->ch[!d]->size){
		rotate(x->ch[d], d), rotate(x, !d);
	} else{
		return;
	}
	Maintain(x, 0), Maintain(x, 1);
}
void insert(SBT* &x, int val){
	if (x == null){
		x = &stack[sz++];
		x->val = val, x->size = 1;
		x->ch[0] = x->ch[1] = null;
	} else {
		int d = val > x->val;
		insert(x->ch[d], val);
		x->push_up();
		Maintain(x, d);
	}
}
int find_kth(SBT *x, int k){
	int t = 0;
	for (; x != null;){
		t = x->ch[0]->size;
		if (k == t + 1) break;
		else if (k <= t) x = x->ch[0];
		else k -= t + 1, x = x->ch[1];
	}
	return x->val;
}
int main(){
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w+", stdout);
#endif
	init();
	int i, t, n, q, val, cnt;
	scanf("%d", &t);
	while (t--){
		sz = cnt = 0;
		init();
		scanf("%d %d", &q, &n);
		printf("%d %d\n", q, (n + 1) >> 1);
		for (i = 1; i <= n; i++){
			scanf("%d", &val);
			insert(root, val);
			if (i % 2 != 0){
				printf("%d ", find_kth(root, ((root->size) >> 1) + 1));
				cnt++;
			}
			if (cnt && cnt % 10 == 0) cnt = 0, printf("\n");
		}
		printf("\n");
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,这是一个关于二叉树遍历的问题。具体来说,给定二叉树的前序遍历和中序遍历,需要求出二叉树的后序遍历。 以下是一个Java实现的例子: ```java import java.util.Scanner; class Node { char value; Node left; Node right; public Node(char value) { this.value = value; } } public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { int n = scanner.nextInt(); String preOrder = scanner.next(); String inOrder = scanner.next(); Node root = buildTree(preOrder, inOrder); postOrder(root); System.out.println(); } scanner.close(); } private static Node buildTree(String preOrder, String inOrder) { if (preOrder.length() == 0) { return null; } char rootValue = preOrder.charAt(0); int rootIndex = inOrder.indexOf(rootValue); Node root = new Node(rootValue); root.left = buildTree(preOrder.substring(1, rootIndex + 1), inOrder.substring(0, rootIndex)); root.right = buildTree(preOrder.substring(rootIndex + 1), inOrder.substring(rootIndex + 1)); return root; } private static void postOrder(Node root) { if (root == null) { return; } postOrder(root.left); postOrder(root.right); System.out.print(root.value); } } ``` 这段代码首先读取输入的测试用例数量t,然后依次读取每个测试用例的节点数量n、前序遍历和中序遍历的字符串。接下来,通过递归构建二叉树,并使用后序遍历输出结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值