Codeforces Round #259 (div2) B solution report,

Source: Internet
Author: User
Tags integer numbers

Codeforces Round #259 (div2) B solution report,

B. Little Pony and Sort by Shifttime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard output

One day, Twilight Sparkle is interested in how to sort a sequence of integersA1, bytes,A2, middle..., middle ,...,ANIn 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, bytes, A2, middle..., middle ,..., A NNavigation → navigation A N, Bytes, A1, bytes, A2, middle..., middle ,..., A NAccept-encoding 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 integerN(2 cores ≤ CoresNLimit ≤0000105). The second line containsNInteger numbersA1, bytes,A2, middle..., middle ,...,AN(1 digit ≤ DigitAILimit ≤ limit 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
22 1
Output
1
Input
31 3 2
Output
-1
Input
21 2
Output
0
Question:

N numbers are given. Each time the last number is moved to the beginning, the final state is required to be a monotonic non-decreasing sequence, and the minimum number of operations is required. If the target cannot be reached, "-1" is output ".

Solution:

It is also a very easy programming basic question to find out the monotonous non-decreasing sequence of the two teams, which is 1 ~ X and x + 1 ~ Y: determines whether the two teams overwrite the entire number and a [n] <= a [1].

A simpler approach is to convert a [1] ~ Copy a [n] to a [1] ~ A [2 * n], and then in 1 ~ In 2 * n, check whether there is a sequence with n numbers that are monotonic and not decreasing.

Code:

#include <cstdio>#define N_max 123456int n, x, y, cnt;int a[N_max];void init() {scanf("%d", &n);for (int i = 1; i <= n; i++)  scanf("%d", &a[i]);}void solve() {for (int i = 1; i <= n; i++)if (a[i] > a[i+1]) {x = i;break;}if (x == n)y = n;elsefor (int i = x+1; i <= n; i++)if (a[i] > a[i+1]) {y = i;break;}if (x == n)printf("0\n");else if (y == n && a[y] <= a[1])printf("%d\n", y-x);elseprintf("-1\n");}int main() {init();solve();}




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.