UVA 120 Stacks of Flapjacks

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

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

 #include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std; int main (){
int a[],b[],c[];
char str[];
while (gets (str)!=NULL){
cout<<str<<endl;
int len;
int cnt=;
len=strlen (str);
memset (a,,sizeof a);
for (int i=;i<len;i++){
while (str[i]!=' '&&i<len)
a[cnt]=a[cnt]*+str[i++]-'';
b[cnt]=a[cnt];
cnt++;
}
cnt--;
sort (b+,b++cnt);
for (int i=;i<=cnt;i++)
c[b[i]]=i;//cout<<a[i]<<" ";
for (int i=;i<=cnt;i++)
a[i]=c[a[i]];//cout<<b[i]<<"r ";cout<<endl;
for (int i=cnt;i>=;i--){
if (a[i]!=i){
if (a[]==i){
cout<<cnt-i+<<" ";
for (int j=;j<i;j++){
b[j]=a[i-j+];
}
for (int j=;j<i;j++)
a[j]=b[j];
}
else{
int temp;
for (int j=;j<=cnt;j++){
if (a[j]==i){
temp=j;break ;
}
}//cout<<temp<<"err";
int f=;
for (int j=i;j>temp;j--)
b[++f]=a[j];
for (int j=;j<temp;j++)
b[++f]=a[j];
for (int j=;j<=f;j++)
a[j]=b[j];
cout<<cnt-temp+<<" "<<cnt-i+<<" ";
}
}
}
cout<<<<endl;
}
return ;
}
上一篇:Spring实现WebSocket通信


下一篇:tomcat 开启远程debug