UVa 679 - Dropping Balls

二叉排序树(Binary Sort Tree)又称二叉查找树(Binary Search Tree),亦称二叉搜索树。 它或者是一棵空树;或者是具有下列性质的二叉树: (1)若左子树不空,则左子树上所有结点的值均小于它的根结点的值; (2)若右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值; (3)左、右子树也分别为二叉排序树;


朴素想法代码TLE(利用二叉树来模拟)

#include <algorithm>
#include <iostream>
#include <cstring>  
#include <cstdio>  
#include <stack>

using namespace std;  

const int N = 20;

int D,I;
int tree[1 << N];

int main(){
    int cas;
    scanf("%d",&cas);
    while(scanf("%d%d",&D, &I) == 2){
        memset(tree, 0, sizeof(tree));
        int n = (1 << D) - 1, k;
        for(int i=0; i<I; i++){
            k = 1;
            for(;;){
                tree[k] = !tree[k];
                k = tree[k] ? (k << 1) : (k << 1 | 1);
                if(k > n) break; 
            }
        }
        printf("%d\n",k/2);
    }
    return 0;
}
找规律AC

当I是奇数,它是向左走的第(1+I)/2个小球;当I是偶数,他是向右走的第I/2个小球。

#include <algorithm>
#include <iostream>
#include <cstring>  
#include <cstdio>  
#include <stack>

using namespace std;  

const int N = 20;

int D,I;
int tree[1 << N];

int main(){
    int cas;
    scanf("%d",&cas);
    while(scanf("%d%d",&D, &I) == 2){
        int k = 1;
        for(int i = 0; i < D - 1; i++){
            if(I & 1){
                k = k << 1;
                I = (1 + I) / 2;
            }
            else{
                k = k << 1 | 1;
                I = I / 2;
            }
        }
        printf("%d\n",k);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值