Codeforces Div. 2 #259-B. Little Pony and Sort by Shift

B. Little Pony and Sort by Shift
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:

a1, a2, ..., an → an, a1, a2, ..., an - 1.

Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?

Input

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).

Output

If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.

Sample test(s)
input
2
2 1
output
1
input
3
1 3 2
output
-1
input
2
1 2
output
0
//AC代码
/*
题意:给你一个无序数列(长度为n)
执行操作:把最后一个元素放在第一个位置,其他元素向后挪一个位置
问执行几次这样的操作使得数列递增(注意不是严格递增元素相等也算递增)
此题想一想就知道只要是符合条件的数列那么只可能存在两个递增子序列并且后一个子序列所有元素不能大于前一个子序列的第一个元素
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=200001;
using namespace std;
int s[Max],k[Max],t[Max];
int main()
{
    int n,m,i,j,p,h;
    while(cin>>n)
    {
        for(i=0;i<n;i++)
            cin>>s[i];
        if(n<=3)
        {
            if(n==1)
                cout<<0<<endl;
            else if(n==2)
            {
                if(s[0]<=s[1])
                    cout<<0<<endl;
                else
                    cout<<1<<endl;
            }
            else if(n==3)
            {
                p=1;
                for(i=1;i<n;i++)
                {
                    if(s[i]>=s[i-1])
                        p+=1;
                }
                if(p==n)//全部递增
                    cout<<0<<endl;
                else if(p==1)
                    cout<<-1<<endl;//全部递减
                else
                {
                    if(s[1]<s[2]&&s[1]<s[0])
                    {
                        if(s[2]>s[0])
                            cout<<-1<<endl;
                        else
                            cout<<2<<endl;
                    }
                    if(s[1]>s[2]&&s[1]>s[0])
                    {
                        if(s[2]>s[0])
                            cout<<-1<<endl;
                        else
                            cout<<1<<endl;
                    }
                }
            }
        }
        else
        {
            p=1;
            for(i=1;i<n;i++)
            {
                if(s[i]>=s[i-1])
                    p+=1;
            }
            if(p==n)//全部递增
            {
                cout<<0<<endl;
            }
            else if(p==1)//全部递减
            {
                cout<<-1<<endl;
            }
            else
            {
                k[0]=s[0];
                m=1;
                for(;m<n;m++)
                {
                    if(s[m]<s[m-1])
                        break;
                    else
                       k[m]=s[m];
                }
                t[0]=s[m];
                h=1;
                for(i=m+1;i<n;i++)
                {
                    if(s[i]<s[i-1])
                        break;
                    else
                        t[h++]=s[i];
                }
                if((h+m)!=n)//不止两个递增子序列,或者一个递增(递减)另一个递减(递增)
                    cout<<-1<<endl;
                else
                {
                    p=0;
                    for(i=0;i<h;i++)
                    {
                        if(t[i]>k[0])
                        {
                            p=1;
                            break;
                        }
                    }
                    if(p==1)
                        cout<<-1<<endl;
                    else
                        cout<<h<<endl;
                }
            }
        }
    }
    return 0;
}
/*
2
2 1
3
1 3 2
2
1 2
5
1 2 3 4 0
5
5 1 2 3 4
7
60 100 120 13 15 55 61
7
60 100 120 13 15 55 59
*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值