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 InblooM at 2016-09-07 15:15:02 on Problem 1033
#include<stdio.h>
#include<string.h>
int A[10001],B[10001],C[10001];
int N,K,s,sum,zong;
void pan(int x)
{

    int hh=0;
    int i,j=0,k=1;
	if(x>=sum)
		return;
	if(B[x]==0)
	{
		for(i=x+1;i<=N;i++)
			if(x==B[i])
			{
				B[x]=x;
				B[i]=0;
				printf("%d %d\n",i,x);
			}
		return;
	}
	if(B[x]!=0)
	{
		memset(C,0,sizeof(C));
		C[x]=1;
		A[k++]=x;
		x=B[x];
		int qp;
		qp=sum;
		while(qp--)
		{
			if(B[x]!=0&&C[B[x]]!=1)
			{
				A[k++]=x;
				C[x]=1;
				x=B[x];
			}
			if(B[x]==0)
			{
				hh=1;
				break;
			}
			if(C[x]==1)
				break;
		}
		if(hh==1)
		{
			for(i=k-1;i>=1;i--)
			{
				B[B[A[i]]]=B[A[i]];
				printf("%d %d\n",A[i],B[A[i]]);
			}
			B[A[1]]=0;
			pan(A[1]);
			return;
		}
		if(hh==0)
		{
			for(i=N;i>=1;i--)
			{
				if(B[i]==0)
				{
					printf("%d %d\n",x,i);
					B[i]=B[x];
					for(j=k-1;j>=1;j--)
                   	{
                    	B[B[A[j]]]=B[A[j]];
                       	printf("%d %d\n",A[j],B[A[j]]);
                    }
                    B[A[1]]=B[i];
					B[i]=0;
					printf("%d %d\n",i,A[1]);
					break;
				}
			}
		}
		return;
	}
}

int main()
{
	int i,k;
	sum=1;
	zong=0;
	memset(B,0,sizeof(B));
	scanf("%d%d",&N,&K);
	while(K--)
	{
		scanf("%d",&s);
		for(i=0;i<s;i++)
		{
			scanf("%d",&k);
			B[k]=sum;
			if(k!=sum)
				zong=1;
			sum++;
		}
	}
	if(zong==0)
		printf("No optimization needed\n");
	for(i=1;i<sum;i++)
	{
		if(B[i]!=i)
			pan(i);
	}		
}

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