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 |
居然是O(n^3)的算法,必挂了,算法优化是AC用的,代码优化是刷时间用的In Reply To:只有计算概论,程序设计实习的基础,怎么省时呢? Posted by:00 at 2005-07-04 10:57:02 > #include<iostream.h> > void main() > { > int N,i,j,k,p; > int *a,*b,*r; > cin>>N; > a=new int[N]; > b=new int[N]; > r=new int[N]; > for(i=1;i<N;i++) > { > cin>>a[i]; > b[i]=i+1; > } > b[0]=1; > for(i=N-1,k=N-1;i>0;i--) > { > if(i==N-1) > { > r[i]=a[i]+1; > for(j=r[i]-1;j<k;j++) > b[j]=b[j+1]; > } > else > { > for(j=0;j<k;j++) > { > int x=0; > for(p=N-1;p>i;p--) > { > if(r[p]<b[j]) > x++; > } > if(b[j]==a[i]+x+1) > { > k--; > r[i]=b[j]; > for(p=j;p<k;p++) > b[p]=b[p+1]; > break; > } > } > } > } > cout<<b[0]<<endl; > for(i=1;i<N;i++) > cout<<r[i]<<endl; > delete [] a; > delete [] b; > delete [] r; > } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator