Red Huang

Red Huang

uva 10821

終於 AC 了 取值構樹,盡量往右邊塞

這樣頭才會最小

//============================================================================  
// Name        : Constructing BST.cpp  
// Date : 2013/3/29 下午3:30:27  
// Author : GCA  
//============================================================================  
#include <iostream>  
#include <cstdio>  
#include <cstring>  
#include <algorithm>  
#include <cmath>  
#include <climits>  
#include <vector>  
#include <set>  
#include <map>  
#include <queue>  
#include <cctype>  
#include <utility>  
using namespace std;  
#ifdef ONLINE\_JUDGE  
#define ll "%lld"  
#else  
#define ll "%I64d"  
#endif  
typedef unsigned int uint;  
typedef long long int Int;  
#define Set(a,s) memset(a,s,sizeof(a))  
#define Write(w) freopen(w,"w",stdout)  
#define Read(r) freopen(r,"r",stdin)  
#define Pln() printf("\\n")  
#define I\_de(x,n)for(Int i=0;i<n;i++)prIntf("%d ",x\[i\]);Pln()  
#define De(x)prIntf(#x"%d\\n",x)  
#define For(i,x)for(Int i=0;i<x;i++)  
#define CON(x,y) x##y  
#define Pmz(dp,nx,ny)for(Int hty=0;hty<ny;hty++){for(Int htx=0;htx<nx;htx++){\\  
    prIntf("%d ",dp\[htx\]\[hty\]);}Pln();}  
#define M 10005  
#define PII pair<Int,Int>  
#define PB push\_back  
#define oo Int\_MAX  
#define Set\_oo 0x3f  
#define Is\_debug true  
#define debug(...) if(Is\_debug)prIntf("DEBUG: "),prIntf(\_\_VA\_ARGS\_\_)  
#define FOR(it,c) for(\_\_typeof((c).begin()) it=(c).begin();it!=(c).end();it++)  
#define eps 1e-6  
bool xdy(double x,double y){return x>y+eps;}  
bool xddy(double x,double y){return x>y-eps;}  
bool xcy(double x,double y){return x<y-eps;}  
bool xcdy(double x,double y){return x<y+eps;}  
Int min3(Int x,Int y,Int z){  
    Int tmp=min(x,y);  
    return min(tmp,z);  
}  
Int max3(Int x,Int y,Int z){  
    Int tmp=max(x,y);  
    return max(tmp,z);  
}  
Int n\[M\];  
Int N,H;  
void rec(Int l,Int r,Int h){  
    if(h>0){  
        Int cont=(1<<(h-1))-1;  
        Int k=r-cont;  
        if(k<l)k=l;  
        if(k>r)k=r;  
        printf(" %lld",k);  
        if(k>l)  
        rec(l,k-1,h-1);  
        if(k<r)  
        rec(k+1,r,h-1);  
    }  
}  
int main() {  
//    freopen("a.txt","w",stdout);  
    ios\_base::sync\_with\_stdio(0);  
    Int ff=0;  
    while(~scanf("%lld%lld",&N,&H)&&N+H){  
        if(N>=(1<<H)){  
            printf("Case %lld: Impossible.\\n",++ff);  
        }else{  
            printf("Case %lld:",++ff);  
            rec(1,N,H);  
            Pln();  
        }  
    }  
  
  
  
}  

加载中...
此文章数据所有权由区块链加密技术和智能合约保障仅归创作者所有。