zoukankan      html  css  js  c++  java
  • codeforces 454B. Little Pony and Sort by Shift 解题报告

    题目链接:http://codeforces.com/problemset/problem/454/B

    题目意思:给出一个序列你 a1, a2, ..., an。 问每次操作只能通过将最后一个数拿出来插到队首,即 a1, a2, ..., an 变成 an, a1, a2, ..., an - 1。求更新后的序列变成非递减的时候,操作了多少次。

        其实是不难的一道题目啦~~~,可能昨天真的太累,脑有点短路,想得太过复杂。

       /****************************************(错误思路)

        竟然用了另一个序列存储最后得到的非递减序列,然后跟原序列比较,看需要多少步骤。

        不过1 3 1 这个 test 一下子毁灭了我的幻想 = =,只能说:乱七八糟啊(读者请忽略)

        (错误代码,这个留给自己借鉴)

         

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cmath>
     6 #include <algorithm>
     7 
     8 using namespace std;
     9 
    10 const int maxn = 1e5 + 5;
    11 int a[maxn], b[maxn];
    12 
    13 int main()
    14 {
    15     int n;
    16     while (scanf("%d", &n) != EOF)
    17     {
    18         for (int i = 0; i < n; i++)
    19         {
    20             scanf("%d", &a[i]);
    21             b[i] = a[i];
    22         }
    23         sort(b, b+n);
    24         int f = 0;
    25         for (int i = 0; i < n; i++)
    26         {
    27             if (a[i] != b[i])
    28             {
    29                 f = 1;
    30                 break;
    31             }
    32         }
    33         if (!f)
    34             printf("0
    ");
    35         else
    36         {
    37             int i, j, k;
    38             for (i = 0; i < n; i++)
    39             {
    40                 if (a[i] == b[0])
    41                     break;
    42             }
    43             int flag = 0;
    44             int ans1 = i;
    45      //       printf("ans1 = %d
    ", ans1);
    46             for (k = 1, j = i+1; j < n && k < n; j++, k++)
    47             {
    48                 if (b[k] != a[j])
    49                 {
    50                     flag = 1;
    51       //              printf("heheh
    ");
    52                 }
    53             }
    54      //       printf("k = %d
    ", k);
    55             if (!flag && j == n)
    56             {
    57                 j = 0;
    58                 for ( ; k+1< n; k++, j++)
    59                 {
    60                     if (b[k] != a[j])
    61                     {
    62                         flag = 1;
    63         //                printf("haha
    ");
    64                     }
    65 
    66                 }
    67             }
    68             int ans2 = j;
    69       //      printf("ans2 = %d
    ", ans2);
    70             if (flag)
    71                 printf("-1
    ");
    72             else
    73                 printf("%d
    ", n-1-ans1+abs(ans1-ans2));
    74         }
    75     }
    76     return 0;
    77 }

    ************************************************************/

    AC 代码:简单快捷 + 容易懂

        

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstdlib>
     4 using namespace std;
     5 
     6 const int maxn = 1e5 + 5;
     7 int a[maxn];
     8 
     9 int main()
    10 {
    11     int n, i, j;
    12     while (scanf("%d", &n) != EOF)
    13     {
    14         for (i = 0; i < n; i++)
    15             scanf("%d", &a[i]);
    16         for (i = 1; a[i-1] <= a[i] && i < n; i++)
    17             ;
    18         for (j = n-1; a[j] >= a[j-1] && j > 0; j--)
    19             ;
    20  //       printf("i = %d, j = %d
    ", i, j);
    21         if (i == n && j == 0)   // 非递减序列
    22             printf("0
    ");
    23         else if (i == j && a[i] <= a[0] && a[n-1] <= a[0])
    24             printf("%d
    ", n-j);
    25         else
    26             printf("-1
    ");
    27     }
    28     return 0;
    29 }
  • 相关阅读:
    Could A New Linux Base For Tablets/Smartphones Succeed In 2017?
    使用libhybris,glibc和bionic共存时的TLS冲突的问题
    6 Open Source Mobile OS Alternatives To Android in 2018
    Using MultiROM
    GPU drivers are written by the GPU IP vendors and they only provide Android drivers
    Jolla Brings Wayland Atop Android GPU Drivers
    How to Use Libhybris and Android GPU Libraries with Mer (Linux) on the Cubieboard
    闲聊Libhybris
    【ARM-Linux开发】wayland和weston的介绍
    Wayland and X.org problem : Why not following the Android Solution ?
  • 原文地址:https://www.cnblogs.com/windysai/p/3886747.html
Copyright © 2011-2022 走看看