每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。

注意半径不是从1开始。数据处理要仔细。

 

 1 #include <iostream>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <cstdio>
 5 using namespace std;
 6 
 7 int main (){
 8     int a[105],b[105],c[105];
 9     char str[1000];
10     while (gets (str)!=NULL){
11         cout<<str<<endl;
12         int len;
13         int cnt=1;
14         len=strlen (str);
15         memset (a,0,sizeof a);
16         for (int i=0;i<len;i++){
17             while (str[i]!=' '&&i<len)
18                 a[cnt]=a[cnt]*10+str[i++]-'0';
19             b[cnt]=a[cnt];
20             cnt++;
21         }
22         cnt--;
23         sort (b+1,b+1+cnt);
24         for (int i=1;i<=cnt;i++)
25             c[b[i]]=i;//cout<<a[i]<<" ";
26         for (int i=1;i<=cnt;i++)
27             a[i]=c[a[i]];//cout<<b[i]<<"r ";cout<<endl;
28         for (int i=cnt;i>=1;i--){
29             if (a[i]!=i){
30                 if (a[1]==i){
31                     cout<<cnt-i+1<<" ";
32                     for (int j=1;j<i;j++){
33                         b[j]=a[i-j+1];
34                     }
35                     for (int j=1;j<i;j++)
36                         a[j]=b[j];
37                 }
38                 else{
39                     int temp;
40                     for (int j=1;j<=cnt;j++){
41                         if (a[j]==i){
42                             temp=j;break ;
43                         }
44                     }//cout<<temp<<"err";
45                     int f=0;
46                     for (int j=i;j>temp;j--)
47                         b[++f]=a[j];
48                     for (int j=1;j<temp;j++)
49                         b[++f]=a[j];
50                     for (int j=1;j<=f;j++)
51                         a[j]=b[j];
52                     cout<<cnt-temp+1<<" "<<cnt-i+1<<" ";
53                 }
54             }
55         }
56         cout<<0<<endl;
57     }
58     return 0;
59 }

 

posted on 2014-07-28 08:48  gfc  阅读(125)  评论(0编辑  收藏  举报