首页 > 其他 > 详细

POJ2309 BST

时间:2015-03-19 00:34:00      阅读:449      评论:0      收藏:0      [点我收藏+]

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

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 }
View Code

 

POJ2309 BST

原文:http://www.cnblogs.com/oimonster/p/4349107.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!