Wednesday, 1 April 2015

UVA 120 Stacks of Flapjacks

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

vector<int>v;

void flipVector(int id)
{
    stack<int>stk;
    int i;
    for(i=0;i<id;i++)
        stk.push(v[i]);
    i=0;
    while(!stk.empty())
    {
        v[i++]=stk.top();
        stk.pop();
    }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n, tc, kk=1, tmp;
    string s;
    while(getline(cin, s))
    {
        vector<int>ans;
        istringstream os(s);
        while(os>>tmp)
            v.push_back(tmp);
        int vl=v.size();
        for(int i=0;i<vl;i++)
        {
            if(i) cout<<" ";
            cout<<v[i];
        }
        cout<<"\n";
        int l=vl;
        for(int i=0;i<l;l--)
        {
            int mx=0, id;
            for(int j=0;j<l;j++)
                if(v[j]>mx)
                    id=j, mx=v[j];
            if(id==l-1) continue;
            //cout<<id<<" "<<l-1<<endl;
            if(id!=0)
            {
                ans.push_back(vl-id);
                flipVector(id+1);
            }
            ans.push_back(vl-l+1);
            flipVector(l);
        }
        for(int i=0;i<ans.size();i++)
            cout<<ans[i]<<" ";
        cout<<"0\n";
        v.clear();
    }
return 0;
}

No comments:

Post a Comment