题目链接: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. noip 2013 luogu P1969 积木大赛

    题目描述 春春幼儿园举办了一年一度的“积木大赛”.今年比赛的内容是搭建一座宽度为n的大厦,大厦可以看成由n块宽度为1的积木组成,第i块积木的最终高度需要是hi. 在搭建开始之前,没有任何积木(可以看成 ...

  2. 蓝桥java 入门训练 Fibonacci数列

    import java.util.Scanner; public class Main{ public static void main(String[] args) { int maxn=10000 ...

  3. Vue.js 和 MVVM

    MVVM 是Model-View-ViewModel 的缩写,它是一种基于前端开发的架构模式,其核心是提供对View 和 ViewModel 的双向数据绑定,这使得ViewModel 的状态改变可以自 ...

  4. 单点登录CAS-Demo

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 1安全证书配置 2部署服务端CAS-Server 3部署CAS-Client 4测试SSO   1,安全证书配置 CAS默认 ...

  5. Js学习第十天----函数

    函数 什么是函数?函数是由事件驱动的或者当他被调用时运行的可反复使用代码块.预计没明确,个人觉得函数就是能完毕一个功能的代码块. 看个案例: <!DOCTYPE html> <htm ...

  6. LeanCloud SDK 中秒杀70%问题的调试方法

    非常多同学在LeanCloud上遇到的不少问题,事实上能够自我解决的,如今介绍一下LeanCloud上的调试方法. LeanCloud 是通过 REST API来进行前后端分离的.这意味着当出现故障的 ...

  7. vim调用python格式化json数据

    vim调用python格式化json数据 November 30, 2013GNU/Linuxpython3, Vimopenwares python有个标准模块叫json,用于编码/解码,序列化/按 ...

  8. Elasticsearch 学习笔记 Elasticsearch及Elasticsearch head安装配置

    一.安装与配置 1.到官网下载Elasticsearch,https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.6. ...

  9. Windows下也能够使用osw追朔系统历史性能

    1.Windows系统历史性能分析困难背景 在Linux/Unix上.要追朔历史性能,一般採用部署nmon进行性能监控採集与存储的方式实现.可是却没有在Windows上的版本号. Windows系统假 ...

  10. Django-权限信息初始化

    数据库 from django.db import models class Menu(models.Model): """ 菜单组: """ ...