CF 53D

题意:

给定两个数组,求由第一个数组到第二个数组的操作方法数 和 步骤(只能相邻间交换)

暴力+模拟

View Code
 1 /*
 2 
 3 */
 4 #include<stdio.h>
 5 #include<string.h>
 6 #include<stdlib.h>
 7 #include<algorithm>
 8 #include<iostream>
 9 #include<queue>
10 //#include<map>
11 #include<math.h>
12 using namespace std;
13 typedef long long ll;
14 //typedef __int64 int64;
15 const int maxn = 305;
16 const int inf = 0x7fffffff;
17 const double pi=acos(-1.0);
18 int a[ maxn ],b[ maxn ];
19 int vis_b[ maxn ];
20 struct node{
21     int x,y;
22 };
23 queue<node>step[ maxn ];
24 void init(){
25     for( int i=0;i<maxn;i++ ){
26         while( !step[ i ].empty() )
27             step[ i ].pop();
28     }
29     memset( vis_b,0,sizeof( vis_b ) );
30 }
31 int main(){
32     int n;
33     while( scanf("%d",&n)!=EOF ){
34         for( int i=0;i<n;i++ )
35             scanf("%d",&a[ i ]);
36         for( int i=0;i<n;i++ )
37             scanf("%d",&b[ i ]);
38         init();
39         int cnt=0;
40         node now;
41         for( int i=0;i<n;i++ ){
42             for( int j=0;j<n;j++ ){
43                 if( vis_b[ j ]==0&&a[ i ]==b[ j ] ){
44                     vis_b[ i ]=1;
45                     if( i==j ){
46                         //vis_b[ j ]=1;
47                         break;
48                     }
49                     else if( i<j ){
50                         //vis_b[ j ]=1;
51                         now.x=j-1,now.y=j;
52                         step[ cnt ].push( now );
53                         swap( b[ now.x ],b[ now.y ] );
54                         while( 1 ){
55                             if( now.x==i ) break;
56                             now.x--;
57                             now.y--;
58                             step[ cnt ].push( now );
59                             swap( b[ now.x ],b[ now.y ] );
60                         }
61                         cnt++;
62                         break;
63                     }
64                     else if( i>j ){
65                         //vis_b[ j ]=1;
66                         now.x=j,now.y=j+1;
67                         step[ cnt ].push( now );
68                         swap( b[ now.x ],b[ now.y ] );
69                         while( 1 ){
70                             if( now.y==i ) break;
71                             now.x++;
72                             now.y++;
73                             step[ cnt ].push( now );
74                             swap( b[ now.x ],b[ now.y ] );
75                         }
76                         cnt++;
77                         break;
78                     }
79                 }
80             }
81         }
82         int ans=0;
83         for( int i=0;i<cnt;i++ )
84             ans+=( step[ i ].size() );
85         printf("%d\n",ans);
86         for( int i=0;i<cnt;i++ ){
87             while( !step[ i ].empty() ){
88                 now=step[ i ].front();
89                 printf("%d %d\n",1+now.x,1+now.y);
90                 step[ i ].pop();
91             } 
92         }
93     }
94     return 0;
95 }

 

posted @ 2013-03-17 14:44  xxx0624  阅读(240)  评论(0编辑  收藏  举报