您的位置:首页 > 其它

UVA 120 Stacks of Flapjacks

2014-07-28 08:48 411 查看
每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。

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

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

int main (){
int a[105],b[105],c[105];
char str[1000];
while (gets (str)!=NULL){
cout<<str<<endl;
int len;
int cnt=1;
len=strlen (str);
memset (a,0,sizeof a);
for (int i=0;i<len;i++){
while (str[i]!=' '&&i<len)
a[cnt]=a[cnt]*10+str[i++]-'0';
b[cnt]=a[cnt];
cnt++;
}
cnt--;
sort (b+1,b+1+cnt);
for (int i=1;i<=cnt;i++)
c[b[i]]=i;//cout<<a[i]<<" ";
for (int i=1;i<=cnt;i++)
a[i]=c[a[i]];//cout<<b[i]<<"r ";cout<<endl;
for (int i=cnt;i>=1;i--){
if (a[i]!=i){
if (a[1]==i){
cout<<cnt-i+1<<" ";
for (int j=1;j<i;j++){
b[j]=a[i-j+1];
}
for (int j=1;j<i;j++)
a[j]=b[j];
}
else{
int temp;
for (int j=1;j<=cnt;j++){
if (a[j]==i){
temp=j;break ;
}
}//cout<<temp<<"err";
int f=0;
for (int j=i;j>temp;j--)
b[++f]=a[j];
for (int j=1;j<temp;j++)
b[++f]=a[j];
for (int j=1;j<=f;j++)
a[j]=b[j];
cout<<cnt-temp+1<<" "<<cnt-i+1<<" ";
}
}
}
cout<<0<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: