zoukankan      html  css  js  c++  java
  • (周日赛)Little Pony and Sort by Shift

    题意及题解:判断有存在几个后面的数比前面的小;
    如果没有则输出0;如果有不止一个,则输出-1;如果恰有一个, 那么就需要判断a[1]和a[n]的大小关系。

    Description
    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 Input
    Input
    2
    2 1
    Output
    1


    Input
    3
    1 3 2
    Output
    -1


    Input
    2
    1 2
    Output
    0

     1 #include<stdio.h>
     2 int main()
     3 {
     4     int n,i,j,t;
     5     int a[100001];
     6     while(~scanf("%d",&n))
     7     {
     8         for(i=0;i<n;i++)
     9         {
    10             scanf("%d",&a[i]);
    11         }
    12         int ans=0;
    13         for(i=1;i<n;i++)
    14         {
    15             if(a[i]<a[i-1])
    16             {
    17                 t=i+1;
    18                 ans++;
    19             }
    20         }
    21         if(ans==0)
    22             puts("0");
    23         else if(ans==1&&a[n-1]<=a[0])
    24         {
    25             printf("%d
    ",n-t+1);
    26         }
    27         else
    28         puts("-1");
    29     }
    30     return 0;
    31 }
  • 相关阅读:
    doker基础用法
    docker容器技术基础入门
    流模式
    装饰器模式
    适配器模式
    组合模式
    桥接模式
    观察者模式
    注册模式
    模板模式
  • 原文地址:https://www.cnblogs.com/awsent/p/4280495.html
Copyright © 2011-2022 走看看