1167 Cartesian Tree

A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.
在这里插入图片描述
Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.

Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.

Output Specification:

For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line

Sample Input:

10
8 15 3 4 1 5 12 10 18 6

Sample Output:

1 3 5 8 4 6 15 10 12 18

思路

一般想法是先建树,再层序遍历,但这题只给出了中序遍历。可以观察到,根节点永远是当前序列的最小值,然后划分左右子树递归建树即可

cpp代码

#include<iostream>
#include<queue>
#include<vector>
#include<unordered_map>
using namespace std;
const int N = 50;
unordered_map<int, int> l, r;
int pos[N];
int getmin(int l, int r) {
	int k = l;
	for (int i = l; i <= r; i++)
		if (pos[i] < pos[k])k = i;

	return k;
}
int build(int inl, int inr) {
	int k = getmin(inl, inr);
	int root = pos[k];
	if (k > inl)l[root] = build(inl, k - 1);
	if (k < inr)r[root] = build(k + 1, inr);
	return root;
}
void bfs(int root) {
	queue<int> q;
	vector<int>vec;
	q.push(root);
	while (q.size()) {
		auto t = q.front();
		q.pop();
		vec.push_back(t);
		if (l.count(t))q.push(l[t]);
		if (r.count(t))q.push(r[t]);
	}
	cout << vec[0];
	for (int i = 1; i < vec.size(); i++)cout << " " << vec[i];
	puts("");
}
int main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)cin >> pos[i];

	int root = build(0, n - 1);

	bfs(root);

	return 0;
}
### 使用Animator和BlendTree进行角色动画混合 在Unity中,`Animator` 和 `Blend Tree` 是用于创建复杂动画过渡和平滑混合的关键工具。通过调整关键帧和权重参数可以创建所需的动画效果[^1]。 #### 创建Blend Tree 为了设置动画融合,在`Animator Controller` 中添加一个新的`Blend Tree`节点。有三种主要类型的Blend Trees可以选择: - **Simple 1D**: 基于单个浮点数输入来线性插值两个或多个剪辑之间的变化。 - **2D Freeform Cartesian/Freeform Directional**: 利用二维坐标系中的X和Y轴作为控制变量来进行多方向的平滑转换。 - **Direct**: 可以直接指定要播放的具体动画片段及其对应的权重比例[^2]。 对于每种类型的选择取决于具体的应用场景以及希望达到的效果。例如,当处理行走周期时可能会采用基于速度矢量的方向型Blend Tree;而对于面部表情则可能更适合使用简单的加权平均方式。 #### 设置根运动(Root Motion) 如果项目涉及到物理移动的角色,则需要考虑启用或者禁用根运动功能。一旦将特定脚本如`RootMotionScript.cs`附加到了目标物体上,并实现了相应的接口方法比如`OnAnimatorMove()`之后,“应用根运动”的属性会依据实际情况被标记为“由脚本处理”。这允许开发者更灵活地管理角色的位置更新逻辑而不必完全依赖内置机制[^3]。 #### 时间轴与起始位置设定 考虑到不同版本间的兼容性和工作流程优化的需求,在较新的Unity版本里引入了一些改进措施使得定义初始姿态变得更加直观便捷。这些改动确保即使是在旧版环境中创作的内容也能顺利迁移至最新平台之上继续发挥效用[^4]。 ```csharp // 示例:简单演示如何配置Animator并利用Blend Tree实现基本动作切换 using UnityEngine; public class CharacterController : MonoBehaviour { private Animator animator; void Start() { animator = GetComponent<Animator>(); // 初始化blend tree所需参数... } void Update() { float horizontalInput = Input.GetAxis("Horizontal"); float verticalInput = Input.GetAxis("Vertical"); // 更新blend tree使用的参数值... animator.SetFloat("Speed", new Vector2(horizontalInput, verticalInput).magnitude); } } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值