Codeforces Sort the Array

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back

typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef map<int, int>mpii;

const int mod = 1000007;
const int high = 100003;
const int inf = 1000005;

int ar[high] , br[high] , up[high];

int main()
{
    fast;
    int n , i , j , x , y;
    while(cin >> n)
    {
        for(i=0; i<n; i++)
        {
            cin >> ar[i];
            br[i] = ar[i];
        }

        sort(br , br+n);

        bool f=false;

        x = y = 0;

        for(i=0; i<n; i++)
        {
            if(!f)
            {
                if(ar[i] != br[i])
                {
                    x = i;
                    f=true;
                }
            }

            else
            {
                if(ar[i] != br[i])
                {
                    y = i;
                }
            }
        }


        //cout << x << " " << y << "\n";

        for(i=x , j=y; i<=y and j>=x; i++ , j--)
        {
            up[i] = ar[j];
        }

        for(i=0; i<x; i++) up[i] = ar[i];
        for(i=y+1; i<n; i++) up[i] = ar[i];
        //for(i=0; i<n; i++) cout << up[i] << " ";
        f=false;

        for(i=0; i<n; i++)
        {
            if(up[i] != br[i])
            {
                f=true;
                break;
            }
        }

        if(f) cout << "no\n";
        else
        {
            outn("yes");
            cout << x+1 << " " << y+1 << "\n";
        }
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number