uva 120 Stacks of Flapjacks 煎饼

先放好第K大的煎饼,然后放好k-1大的煎饼。


因为如果要放对第k-1大的煎饼的位置,所有被变更位置的煎饼一定在区间[1,k-1]内,不会影响到刚才放好的第k大煎饼。


/**==========================================
 *   This is a solution for ACM/ICPC problem
 *
 *   @source£º
 *   @type:
 *   @author: wust_ysk
 *   @blog:  http://blog.csdn.net/yskyskyer123
 *   @email: 2530094312@qq.com
 *===========================================*/
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<sstream>
using namespace std;
typedef long long ll;
const int INF =0x3f3f3f3f;
const int maxn=30    ;
int n,cnt;
struct A
{
    int num;
    bool operator<(const A y)const
    {
        return num<y.num;
    }
}a[maxn+5],b[maxn+5];
int pos;
string s;



void print(int x)
{
    if(cnt++)  putchar(' ');
    printf("%d",n+1-x);

}
void get(int k)
{
    if(pos==k)    return  ;
    if(pos==1)
    {
        print(k);
        reverse(a+1,a+k+1);

        return;
    }
    print(pos);
    reverse(a+1,a+pos+1);

    print(k);
    reverse(a+1,a+k+1);

}
void work()
{
    memcpy(b,a,sizeof a);
    sort(b+1,b+1+n);

    for(int i=n;i>=1;i--)
    {

          int maxi=-1;
         for(int j=1;j<=i;j++)
        {
           if(a[j].num>maxi)
           {
               maxi=a[j].num;
               pos=j;
           }

        }
        get(i);

    }
    print(n+1-0);
    putchar('\n');

}
int main()
{
   while(getline(cin,s))
   {
       n=0;
       cnt=0;
       stringstream ss(s);
       while(ss>>a[++n].num)
       {
           if(n!=1) putchar(' ');
          printf("%d",a[n].num);
       }
       putchar('\n');
       n--;
       work();
   }
   return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值