| ||||||||||
| 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 | |||||||||
排列高手指教下:
本人用c++能计算出结果,但程序运行时间超出要求
以下是我写的几个函数:
void swap(int x){
int y=n-1;
while(x<y){
int t=p[x];
p[x]=p[y];
p[y]=t;
x++;y--;
}
}
void swap(int x,int y){
int t=p[x];
p[x]=p[y];
p[y]=t;
}
void exchange(int x){
while(p[x]<p[x+1]&&x<n-1){
int t=p[x];
p[x]=p[x+1];
p[x+1]=t;
x++;
}
}
void fun(){
int x,y,i,t;
while(1){
for(x=n-1;p[x-1]>p[x]&&x>0;x--);
for(y=n-1;p[x-1]>p[y];y--);
for(t=i=1;i<=n-x;i++)
t*=i;
if(k>(y-x+1)*t)}
exchange(x-1);
k-=(y-x+1)*t;}
else break;
}
while(k--){
for(x=n-1;p[x-1]>p[x]&&x>0;x--);
if(x==0)swap(0);
else {
for(y=n-1;p[x-1]>p[y];y--);
swap(x-1,y);
swap(x);}
//disp();
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator