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

Re:高手指点:怎样改才不超时,郁闷中.......

Posted by dhu_try at 2009-04-06 20:17:16 on Problem 1833
In Reply To:高手指点:怎样改才不超时,郁闷中....... Posted by:jinweiliang at 2009-04-05 15:56:25
> // 1833.cpp : Defines the entry point for the console application.
> //
> 
> //#include "stdafx.h"
> #include <stdio.h>
> 
> 
> #include<algorithm>
> using namespace std;
> 
> int main()
> {
> 	int m;
> 	//freopen("1833.txt","r",stdin);
> 
> 	scanf("%d",&m);
> 	while(m--)
> 	{
> 		int n,j,k,i,a[1025];
> 		scanf("%d %d",&n,&k);
> 		for(i=0;i<n;i++)
> 		{
> 			scanf("%d",&a[i]);
> 			//printf("%d",a[i]);
> 		}//printf("\n");
> 
> 		 i=0;
> 		j=1;
> 		while(1) 
> 		{
> 			if(!next_permutation(a,a+n))//产生当前排列的后一个排列
> 			{
> 				for(i=1;i<n;i++)
> 				printf("%d ",i);
> 				printf("%d\n",i);
> 				goto loop;
> 			}
> 			else if(next_permutation(a,a+n))
> 			{
> 				j++;
> 				if(j==k)
> 				{
> 					for(i=0;i<n-1;i++)
> 					printf("%d ",a[i]);
> 					printf("%d\n",a[i]);
> 					goto p2;
> 				}
> 			}
> 		}
> 		p2:;
> 		loop:;
> 	
> 	}
> 	return 0;
> }
> 

直接用 next_permutation() K次就可以了呀~~
        for(i=0;i<k;i++)
        {
            next_permutation(p,p+n);
        }

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