UVA 120 Stacks of Flapjacks
2014-07-28 08:48
411 查看
每次从最底部开始处理,如果不是最大值,则把最大值翻到底部。这就是最优解。原理自己模拟一下就好。。。
注意半径不是从1开始。数据处理要仔细。
注意半径不是从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; }
相关文章推荐
- uva 120 - Stacks of Flapjacks (RE)
- UVA 120 Stacks of Flapjacks (模拟+贪心)
- Uva 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVA-120- Stacks of Flapjacks
- UVA - 120 Stacks of Flapjacks
- UVa 120 - Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks ——思路题
- Uva120 Stacks of Flapjacks 翻煎饼
- UVa 120 Stacks of Flapjacks
- △UVA120 - Stacks of Flapjacks
- UVA 120 - Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- UVA - 120 Stacks of Flapjacks
- UVA 120 Stacks of Flapjacks
- Uva120 Stacks of Flapjacks 翻煎饼
- Stacks of Flapjacks 翻煎饼-UVA120 白书第五章
- UVA 120 Stacks of Flapjacks