CF454B.Little Pony and Sort by Shift

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

One day, Twilight Sparkle is interested in how to sort a sequence of integers _a_1, _a_2, ..., a__n 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:

_a_1, _a_2, ..., a__n → a__n, _a_1, _a_2, ..., a__n - 1.

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

一天,暮光闪闪对如何将一串整数 a1,a2,…,ana_1, a_2, \dots, a_n 按非递减顺序排序产生了兴趣。作为一名年轻的独角兽,她唯一能执行的操作是单位移位:即将序列的最后一个元素移动到最前面:

a1,a2,…,an→an,a1,a2,…,an−1.a_1, a_2, \dots, a_n \rightarrow a_n, a_1, a_2, \dots, a_{n-1}.

请帮助暮光闪闪计算:她需要执行的最少操作次数是多少?

输入格式

The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers _a_1, _a_2, ..., a__n (1 ≤ a__i ≤ 105).

第一行包含一个整数 nn(2 ≤ n ≤ 1052 \leq n \leq 10^5)。第二行包含 nn 个整数 a1, a2, ..., ana_1, a_2, ..., a_n(1 ≤ ai ≤ 1051 \leq a_i \leq 10^5)。

输出格式

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

如果无法将序列排序,则输出 −1-1。否则,输出暮光闪闪将其排序所需的最少操作次数。

输入输出样例

  • 输入#1

    2
    2 1

    输出#1

    1
  • 输入#2

    3
    1 3 2

    输出#2

    -1
  • 输入#3

    2
    1 2

    输出#3

    0

输入解题思路,AI测评打分。不知道怎么写?

首页