Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

排列

Posted by lizhaogui at 2010-06-20 15:26:35 on Problem 1833
高手指教下:
本人用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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator