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 |
这题可以用贪心的~~~不需要递归~~~懒得写解题报告,就贴代码吧 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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator