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 yzhw at 2009-01-23 11:48:36 on Problem 1033
懒得写解题报告,就贴代码吧
import java.util.*;
public class acm1033 {

	static int search(int flag,int[] ori,int num)
	{
	  for(int i=1;i<=num;i++)
		  if(flag==ori[i]) return i;
	  return 0;
	}
	public static void main(String[] args) {
		Scanner in=new Scanner(System.in);
		int num,fnum,start=1,count=0;
		int[] ori=new int[10001];
		int[] fin=new int[10001];
		Arrays.fill(ori, 0);
		Arrays.fill(fin, 0);
		num=in.nextInt();
		fnum=in.nextInt();
		for(int i=1;i<=fnum;i++)
		{
			int pnum=in.nextInt();
			for(int j=start;start<j+pnum;start++)
			{
				ori[in.nextInt()]=start;			
			}
		}
		for(int i=1;i<start;i++) fin[i]=i;
		for(int i=1;i<start;i++)
		{
			if(ori[i]==fin[i]) count++;
		}

		if(count==start-1) System.out.println("No optimization needed");
		else 
		{
           int free=0,i;
           count=start-count-1;
			for(i=1;i<=num;i++) 
				if(ori[i]==0) 
				  {
					free=i;
					break;
				  }
			
			for(;count>0;)
			{
				boolean flag=false;
				for(i=1;i<start;i++)
				{
					if(ori[i]==0) 
					{
						
						int from=search(i,ori,num);
						int to=i;
						System.out.println(from+" "+to);
						ori[to]=ori[from];
						ori[from]=0;
						if(free==to) free=from;
						count--;
						flag=true;
			
					}
				}
				if(!flag) 
				{
				  for(i=1;i<start;i++)
					  if(ori[i]!=fin[i]) break;
					System.out.println(i+" "+free);
					ori[free]=ori[i];
					ori[i]=0;
					free=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