POJ2309 BST

这题难度还可以,找出规律就不难了

PS:x的n次方是 pow(x,n) 而且x必须是double or float.

BST

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8850   Accepted: 5400

Description

Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we can get the minimum number in this subtree by repeating going down the left node until the last level, and we can also find the maximum number by going down the right node. Now you are given some queries as "What are the minimum and maximum numbers in the subtree whose root node is X?" Please try to find answers for there queries. 

Input

In the input, the first line contains an integer N, which represents the number of queries. In the next N lines, each contains a number representing a subtree with root number X (1 <= X <= 231 - 1).

Output

There are N lines in total, the i-th of which contains the answer for the i-th query.

Sample Input

2
8
10

Sample Output

1 15
9 11

Source

POJ Monthly,Minkerui

 1 //oimonster
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<iostream>
 6 using namespace std;
 7 int a[101],b[101],c[101];
 8 int main(){
 9     int i,j,n,p,t;
10     scanf("%d",&t);
11     while(t>0){
12         t--;
13         scanf("%d",&n);
14         p=n;
15         i=0;
16         while(p!=0){
17             i++;
18             a[i]=p%2;
19             p/=2;
20         }
21         n=i;
22         for(i=1;i<=n;i++){
23             b[i]=a[i];
24             c[i]=a[i];
25         }
26         j=1;
27         while(a[j]==0){
28             j++;
29         }
30         b[j]=0;
31         b[1]=1;
32         for(i=1;i<=j-1;i++){
33             c[i]=1;
34         }
35         int ans1,ans2;
36         ans1=ans2=0;
37         for(i=1;i<=n;i++){
38             ans1+=pow(2.0,i-1)*b[i];
39             ans2+=pow(2.0,i-1)*c[i];
40         }
41         printf("%d %d\n",ans1,ans2);
42     }
43     return 0;
44 }

时间: 2024-10-02 21:08:50

POJ2309 BST的相关文章

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

验证给定序列是否是BST的preoder序列

from leetcode https://leetcode.com/problems/verify-preorder-sequence-in-binary-search-tree/ 比如序列 2, 1, 3 是如下图的BST的preorder 序列: 但是2, 3, 1就不会是一个preorder序列: 先复习一下BST,给定一个节点,其左子树的所有节点都小于该节点,右子树的所有节点都大于该节点:preorder序列是指在遍历该BST的时候,先记录根节点,再遍历左子树,然后遍历右子树:所以一个

笔试算法题(09):查找指定和值的两个数 &amp; 构造BST镜像树

出题:输入一个已经升序排序的数组和一个数字:要求在数组中查找两个数,这两个数的和正好等于输入的那个数字,输出任意一对数字就可以,要求时间复杂度是O(n): 分析:对于升序排序的数组{-i-j-k-m--},只有可能是i+m=j+k(j和k可能是同一个数),所以可以从两边往中间收缩而忽视其他交叉相加的情况: 解题: 1 void FindSumFactor(int *array, int length, int sum) { 2 int left=0, right=length-1; 3 whil

二叉查找树BST 模板

二叉查找树BST 就是二叉搜索树 二叉排序树. 就是满足 左儿子<父节点<右儿子 的一颗树,插入和查询复杂度最好情况都是logN的,写起来很简单. 根据BST的性质可以很好的解决这些东西 1.查询值 int Search(int k,int x) { if(x<a[k].key && a[k].l) Search(a[k].l,x); else if(x>a[k].key && a[k].r) Search(a[k].r,x); else retur

[LeetCode] Largest BST Subtree 最大的二分搜索子树

Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest means subtree with largest number of nodes in it. Note:A subtree must include all of its descendants.Here's an example: 10 / 5 15 / \ \ 1 8 7 The Largest

[LeetCode] Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

Leetcode 530. Minimum Absolute Difference in BST

Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 3 / 2 Output: 1 Explanation: The minimum absolute difference is 1, which is the difference between 2 and 1 (o

HashMap VS BST

HashMap usually performs search operations bounded with complexity of O(1)<=T(n)<=O(n). BST performs search operations in O(logN)<=T(n)<=O(N). HashMap is implemented with array and hash function. HashMap has O(1) lookup time when the hashcode

230. Kth Smallest Element in a BST

题目: Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements. Follow up:What if the BST is modified (insert/delete operations) often and you