Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怀疑bst会退化,O(n^2)就挂了In Reply To:nlogn 的算法也超时????郁闷了 Posted by:fxzy at 2006-01-11 18:15:39 > 我的程序:用二叉排序树做的 > #include<stdio.h> > #include<stdlib.h> > #define maxstar 15001 > typedef struct star > { > int date; > int lev; > int lchild,rchild; > }; > > int root=-1; > int level; > int num[maxstar]; > star stars[maxstar]; > > void insert(int x,int i) > { > int p,q; > p=i; > if(root==-1) > {root=0;stars[root].date=x;stars[root].lev=1;stars[root].lchild=-1;stars[root].rchild=-1;num[level]++;return;} > > q=root; > while(1) > { > if(stars[q].date==x) > {level+=stars[q].lev;num[level]++;stars[q].lev++;return;} > if(stars[q].date>x) > { > if(stars[q].lchild!=-1) > {stars[q].lev++;q=stars[q].lchild;continue;} > else {stars[q].lev++;stars[q].lchild=p;stars[p].date=x;stars[p].lchild=-1;stars[p].rchild=-1;stars[p].lev=1;num[level]++;return;} > } > if(stars[q].date<x) > { > if(stars[q].rchild!=-1) > {level+=stars[q].lev;q=stars[q].rchild;continue;} > else {level+=stars[q].lev;stars[p].date=x;num[level]++;stars[q].rchild=p;stars[p].lchild=-1;stars[p].rchild=-1;stars[p].lev=1;return;} > } > } > > > } > > int main() > { int x,y,n,i; > scanf("%d",&n); > for(i=0;i<=n-1;i++) > { > scanf("%d%d",&x,&y); > level=0; > insert(x,i); > } > for(i=0;i<=n-1;i++) > printf("%d\n",num[i]); > } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator