終於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();
}
}
}
留言
張貼留言