题解
暴力判断。
首先看数列有几个坡(坡为非减的序列),若大于两个,则无解输出-1.若有两个,则判断a[n]和a[1]的大小关系。若只有一个,则输出0。
#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
int n,a[100002],ct,w;
void init()
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{scanf("%d",&a[i]);
if(a[i]>=a[i-1]) ct++;
else w=i-1;
}
//printf("%d\n",ct);
}
void work()
{
if(ct==n) {printf("0\n");}
else if(ct<n-1) {printf("-1\n");}
else
{if(a[n]<=a[1]) printf("%d\n",n-w);
else printf("-1\n");
}
}
int main()
{
init(); work();
return 0;
}