PAT甲级 1167 Cartesian Tree

2024-01-20 18:59
文章标签 tree pat 甲级 cartesian 1167

本文主要是介绍PAT甲级 1167 Cartesian Tree,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

已知是heaporderinorder排序,然后gen树,要判断在inorder前一个节点的左右,就可以左右依次gen,得到相应的内容;

也可以不用gen树拿到最小值和index直接保存层序遍历,最小堆,最小值就是根节点划开左右branch,使用map自动排序的特点

不能使用数组存储res(10741824, -999)

res[index] = v[pot];

会越界,要使用map存储并自动排序

update1

#include<iostream>
#include<unordered_map>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int m, cnt = 0;
vector<int> arr, cp;
unordered_map<int, int> ump;
struct nod{nod *l=NULL, *r=NULL; int val=-99;};
nod *recursion(nod *root, int start) {if(root==NULL) {root = new(nod);root->val = cp[start];}if(ump[cp[start]] < ump[root->val]) root->l = recursion(root->l, start);if(ump[cp[start]] > ump[root->val]) root->r = recursion(root->r, start);return root;
}
int main(int argc, char **argv){int i, j, k, n;cin>>m;for(i = 0; i < m; i++){cin>>k;arr.push_back(k);ump[k] = i;}cp = arr;sort(cp.begin(), cp.end());nod *root = NULL;for(i = 0; i < m; i++)root = recursion(root, i);vector<int> lev;queue<nod *> q;q.push(root);while(q.size()!=0) {nod *tmp = q.front();if(tmp->l!=NULL) q.push(tmp->l);if(tmp->r!=NULL) q.push(tmp->r);lev.push_back(tmp->val);q.pop();}printf("%d", lev[0]);for(i = 1; i< lev.size(); i++) printf(" %d", lev[i]);return EXIT_SUCCESS;
}

old before 

#include<iostream>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include<queue>
using namespace std;
vector<int> v;
unordered_map<int, int> mp;
struct nod{nod *l=NULL, *r=NULL; int val;};
nod* generate(nod *root, int val) {int ind = mp[val];if(root==NULL){root = new(nod);root->val = val;}else if(ind < mp[root->val])root->l = generate(root->l, val);else if(ind > mp[root->val])root->r = generate(root->r, val);return root;
}
int main(void) {int i, j, k, m, n;cin>>m;for(i = 0; i < m; i++) {cin>>n;v.push_back(n);mp[n] = i;}sort(v.begin(), v.end());nod *nd, *root = NULL;for(i = 0; i < v.size(); i++)root = generate(root, v[i]);queue<nod *>q;q.push(root);while(q.size() > 0) {nd = q.front();if(nd->l!=NULL) q.push(nd->l);if(nd->r!=NULL) q.push(nd->r);cout<<nd->val;if(q.size()!=1) cout<<" ";q.pop();}return 0;
}

update2

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
int m, lev[1000];
vector<int> arr;
map<int, int> mp;
//树的遍历和产生key是split 左右branch的条件,然后将根节点入栈
void recursion(int index, int l, int r) {//最小堆,最小值就是根节点划开左右branchif(l >= r) return;int minmin = 999999999, id=l;for(int i = l; i < r; i++) {if(minmin > arr[i]) {minmin = arr[i];id = i;}}mp[index] = arr[id];recursion(index * 2, l, id);recursion(index * 2 + 1, id + 1, r);
}
int main(int argc, char **argv){int i, j, k, n, maxmax = -999999999;cin>>m;for(i = 0; i < m; i++){cin>>k;arr.push_back(k);if(maxmax < k) maxmax = k;}recursion(1, 0, m);for(map<int, int>::iterator it=mp.begin(); it!=mp.end(); it++) {printf("%d", it->second);if(it->second!=maxmax) printf(" ");}return EXIT_SUCCESS;
}


GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.https://github.com/ZouJiu1/PAT

这篇关于PAT甲级 1167 Cartesian Tree的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/627001

相关文章

树(Tree)——《啊哈!算法》

树 什么是树?树是一种特殊的图,不包含回路的连通无向树。 正因为树有着“不包含回路”这个特点,所以树就被赋予了很多特性。 一棵树中的任意两个结点有且仅有唯一的一条路径连通。一棵树如果有n个结点,那么它一定恰好有n-1条边。在一棵树中加一条边将会构成一个回路。 一棵树有且只有一个根结点。 没有父结点的结点称为根结点(祖先)。没有子结点的结点称为叶结点。如果一个结点既不是根结点也不是叶

226 Invert Binary Tree

//226 Invert Binary Tree//算法思路:主要使用递归算法public class Solution {public TreeNode invertTree(TreeNode root) {//1 出口 空节点if (root==null)return null;//2 递归 调用自己TreeNode left = root.left;TreeNode right = ro

PAT甲级-1044 Shopping in Mars

题目   题目大意 一串项链上有n个钻石,输入给出每个钻石的价格。用m元买一个连续的项链子串(子串长度可为1),如果不能恰好花掉m元,就要找到最小的大于m的子串,如果有重复就输出多个,按递增顺序输出子串的前端和后端索引。 原来的思路 取连续的子串使和恰等于m,没有恰等于就找最小的大于。可以将子串依次累加,使得每个位置都是起始位置到该位置的序列和,整个数组显递增顺序,就可以用右边减左边

Sorry!Hbase的LSM Tree就是可以为所欲为!

我们先抛出一个问题: LSM树是HBase里使用的非常有创意的一种数据结构。在有代表性的关系型数据库如MySQL、SQL Server、Oracle中,数据存储与索引的基本结构就是我们耳熟能详的B树和B+树。而在一些主流的NoSQL数据库如HBase、Cassandra、LevelDB、RocksDB中,则是使用日志结构合并树(Log-structured Merge Tree,LSM Tr

【spring】does not have member field ‘com.sun.tools.javac.tree.JCTree qualid

spring-in-action-6-samples 的JDK版本 最小是11,我使用 了22: jdk21 jdk22 都与lombok 不兼容,必须使用兼容版本, 否则报错: thingsboard 的大神解释了: java: java.lang.NoSuchFieldError: Class com

PAT (Advanced Level) Practice——1011,1012

1011:  链接: 1011 World Cup Betting - PAT (Advanced Level) Practice (pintia.cn) 题意及解题思路: 简单来说就是给你3行数字,每一行都是按照W,T,L的顺序给出相应的赔率。我们需要找到每一行的W,T,L当中最大的一个数,累乘的结果再乘以0.65,按照例子写出表达式即可。 同时还需要记录每一次选择的是W,T还是L

[LeetCode] 863. All Nodes Distance K in Binary Tree

题:https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/ 题目大意 求给树中,距给定 结点 指定长度的 所有结点的val 思路 tree -> graph 、 bfs 先遍历树,并用map记录每个结点的父结点 ,将树变为图,然后 bfs。 /*** Definition for a binary tree

js实现树级递归,通过js生成tree树形菜单(递归算法)

1、效果图 需求:首先这是一个数据集—js的类型,我们需要把生成一个tree形式的对象 : var data = [{ id: 1, name: "办公管理", pid: 0 },{ id: 2, name: "请假申请", pid: 1 },{ id: 3, name: "出差申请", pid: 1 },{ id: 4, name: "请假记录", pid: 2 },{ id:

【unity实战】利用Root Motion+Blend Tree+Input System+Cinemachine制作一个简单的角色控制器

文章目录 前言动画设置Blend Tree配置角色添加刚体和碰撞体代码控制人物移动那么我们接下来调整一下相机的视角效果参考完结 前言 Input System知识参考: 【推荐100个unity插件之18】Unity 新版输入系统Input System的使用,看这篇就够了 Cinemachine虚拟相机知识参考: 【推荐100个unity插件之10】Unity最全的最详细的C

树链剖分 + 后缀数组 - E. Misha and LCP on Tree

E. Misha and LCP on Tree  Problem's Link   Mean:  给出一棵树,每个结点上有一个字母。每个询问给出两个路径,问这两个路径的串的最长公共前缀。 analyse: 做法:树链剖分+后缀数组. 记录每条链的串,正反都需要标记,组成一个长串. 然后记录每条链对应的串在大串中的位置,对大串求后缀数组,最后询问就是在一些链