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

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

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

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

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

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

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

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm> using namespace std; const int maxn = 1e5 + ;
int a[maxn], b[maxn]; int main()
{
int n;
while (scanf("%d", &n) != EOF)
{
for (int i = ; i < n; i++)
{
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b, b+n);
int f = ;
for (int i = ; i < n; i++)
{
if (a[i] != b[i])
{
f = ;
break;
}
}
if (!f)
printf("0\n");
else
{
int i, j, k;
for (i = ; i < n; i++)
{
if (a[i] == b[])
break;
}
int flag = ;
int ans1 = i;
// printf("ans1 = %d\n", ans1);
for (k = , j = i+; j < n && k < n; j++, k++)
{
if (b[k] != a[j])
{
flag = ;
// printf("heheh\n");
}
}
// printf("k = %d\n", k);
if (!flag && j == n)
{
j = ;
for ( ; k+< n; k++, j++)
{
if (b[k] != a[j])
{
flag = ;
// printf("haha\n");
} }
}
int ans2 = j;
// printf("ans2 = %d\n", ans2);
if (flag)
printf("-1\n");
else
printf("%d\n", n--ans1+abs(ans1-ans2));
}
}
return ;
}

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

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

 #include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std; const int maxn = 1e5 + ;
int a[maxn]; int main()
{
int n, i, j;
while (scanf("%d", &n) != EOF)
{
for (i = ; i < n; i++)
scanf("%d", &a[i]);
for (i = ; a[i-] <= a[i] && i < n; i++)
;
for (j = n-; a[j] >= a[j-] && j > ; j--)
;
// printf("i = %d, j = %d\n", i, j);
if (i == n && j == ) // 非递减序列
printf("0\n");
else if (i == j && a[i] <= a[] && a[n-] <= a[])
printf("%d\n", n-j);
else
printf("-1\n");
}
return ;
}

codeforces 454B. Little Pony and Sort by Shift 解题报告的更多相关文章

  1. Codeforces 259 B - Little Pony and Sort by Shift

    题目链接:http://codeforces.com/contest/454/problem/B 解题报告:太渣了,这个模拟题最后跑大数据的时候挂了,最后还花了很久才过,用的最笨的方法,直接模拟,代码 ...

  2. codeforces——Little Pony and Sort by Shift

    /* 题目大意:给你一个序列,不断地将最后边的数值移动到最前边,问最少经过多少次可以变成一个单调递增的序列! 如果不能则输出-1. 如果该序列按照不断从后向前移动排序成功,那么该序列要么只有一个单调递 ...

  3. [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)

    interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...

  4. codeforces 519C. A and B and Team Training 解题报告

    题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个  experienced participants  和 m 个 newbie ...

  5. codeforces 489C.Given Length and Sum of Digits... 解题报告

    题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...

  6. Lintcode: Sort Colors II 解题报告

    Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...

  7. 【LeetCode】912. Sort an Array 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...

  8. Codeforces Round #335 (Div. 2)B. Testing Robots解题报告

                                                                                               B. Testin ...

  9. BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...

随机推荐

  1. 从dataset表中获取某一列的所有值方法

    原文发布时间为:2008-07-31 -- 来源于本人的百度文章 [由搬家工具导入] 可以datarow遍历所有行即可,如下:pubauthor这个表中的au_lname的所有值加到listbox上面 ...

  2. Codeforces 713C Sonya and Problem Wihtout a Legend(DP)

    题目链接   Sonya and Problem Wihtout a Legend 题意  给定一个长度为n的序列,你可以对每个元素进行$+1$或$-1$的操作,每次操作代价为$1$. 求把原序列变成 ...

  3. jsp 时间格式

    <%@ taglib prefix='fmt' uri="http://java.sun.com/jsp/jstl/fmt" %> <fmt:formatDate ...

  4. Codeforces 959 D Mahmoud and Ehab and another array construction task

    Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of ...

  5. Java全局变量不加修饰符时的访问权限范围

    如上图所示.

  6. iOS中创建自定义的圆角按钮

    iOS中很多时候都需要用到指定风格的圆角按钮,尽管UIButton提供了一个方式创建圆角按钮: + (id)buttonWithType:(UIButtonType)buttonType;//指定bu ...

  7. setImageEdgeInsets 和 setImage配合使用达到button区域大并可调节其上图片显示区域大小的效果

    [self.indicator setImage:[UIImage imageNamed:@"01_login_moreicon@2x.png"] forState:UIContr ...

  8. android开发教程之使用线程实现视图平滑滚动示例 改

    package com.melonsapp.messenger.ui.popupuser; import android.os.Handler; import android.view.View; i ...

  9. flask如何处理并发

    1.使用自身服务器的多进程或者多线程,参考werkzeug的run_simple函数的入参.注意,进程和线程不能同时开启 2.使用gunicorn使用多进程,-w worker 进程数,类型于运行多个 ...

  10. 修改ubuntu系统时区

    ubuntu默认时区是Etc/UTC,和我们的北京时间相差8个时区,需要修改系统的时区,以下有两种简单方式修改系统时区: 1.修改/etc/timezone文件 vi /etc/timezone 把E ...