• (周日赛)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 }
  • 相关阅读:
    python的使用
    SFM(structure from motion)
    linux 常用命令
    opencv图像操作
    两圆位置判断
    nat123动态域名解析软件使用教程
    IIS负载均衡
    Oracle 查询表信息(字段+备注) .
    【原创】开源.NET排列组合组件KwCombinatorics使用(三)——笛卡尔积组合
    visual studio 2013使用技巧
  • 原文地址:https://www.cnblogs.com/awsent/p/4280495.html
Copyright © 2020-2023  润新知