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 一下子毁灭了我的幻想 = =,只能说:乱七八糟啊(读者请忽略)
(错误代码,这个留给自己借鉴)
#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 解题报告的更多相关文章
- Codeforces 259 B - Little Pony and Sort by Shift
题目链接:http://codeforces.com/contest/454/problem/B 解题报告:太渣了,这个模拟题最后跑大数据的时候挂了,最后还花了很久才过,用的最笨的方法,直接模拟,代码 ...
- codeforces——Little Pony and Sort by Shift
/* 题目大意:给你一个序列,不断地将最后边的数值移动到最前边,问最少经过多少次可以变成一个单调递增的序列! 如果不能则输出-1. 如果该序列按照不断从后向前移动排序成功,那么该序列要么只有一个单调递 ...
- [codeforces contest 1119 F] Niyaz and Small Degrees 解题报告 (树形DP+堆)
interlinkage: http://codeforces.com/contest/1119/problem/F description: 有一颗$n$个节点的树,每条边有一个边权 对于一个$x$ ...
- codeforces 519C. A and B and Team Training 解题报告
题目链接:http://codeforces.com/contest/519/problem/C 题目意思:给出 n 个 experienced participants 和 m 个 newbie ...
- codeforces 489C.Given Length and Sum of Digits... 解题报告
题目链接:http://codeforces.com/problemset/problem/489/C 题目意思:给出 m 和 s,需要构造最大和最小的数.满足长度都为 m,每一位的数字之和等于 s. ...
- Lintcode: Sort Colors II 解题报告
Sort Colors II 原题链接: http://lintcode.com/zh-cn/problem/sort-colors-ii/# Given an array of n objects ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
- Codeforces Round #335 (Div. 2)B. Testing Robots解题报告
B. Testin ...
- BestCoder7 1001 Little Pony and Permutation(hdu 4985) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4985 题目意思:有 n 个数,对于第 i 个数给出 σ(i) 的值.求出互不相交的循环的个数,并输出每 ...
随机推荐
- android Paint属性
** * Paint类介绍 * * Paint即画笔,在绘图过程中起到了极其重要的作用,画笔主要保存了颜色, * 样式等绘制信息,指定了如何绘制文本和图形 ...
- 图说OSI七层网络模型
开放式系统互联通信参考模型(英语:Open System Interconnection Reference Model,缩写为 OSI),简称为OSI模型(OSI model),一种概念模型,由国际 ...
- 【深入Java虚拟机】之六:Java语法糖
语法糖(Syntactic Sugar),也称糖衣语法,是由英国计算机学家Peter.J.Landin发明的一个术语,指在计算机语言中添加的某种语法,这种语法对语言的功能并没有影响,但是更方便程序员使 ...
- 【Java TCP/IP Socket】Java NIO Socket VS 标准IO Socket
简介 Java NIO从JDK1.4引入,它提供了与标准IO完全不同的工作方式. NIO包(java.nio.*)引入了四个关键的抽象数据类型,它们共同解决传统的I/O类中的一些问题. 1. ...
- Hash分析
分析Hash 列表内容 Hash表中的一些原理/概念,及依据这些原理/概念,自己设计一个用来存放/查找数据的Hash表,而且与JDK中的HashMap类进行比較. 我们分一下七个步骤来进行. Hash ...
- HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- hive:Access denied for user 'root'@'%'
配置hive全分布模式时候,在mysql里面创建用户:create user 'hive' identified by 'hive'; 然后给hive帐号分配全部权限: grant all privi ...
- mysql 查询正在执行的事务以及等待锁 常用的sql语句
使用navicat测试学习: 首先使用set autocommit = 0;(取消自动提交,则当执行语句commit或者rollback执行提交事务或者回滚) 在打开一个执行update查询 正在 ...
- ++*p,(*p)++,*p++与*++p四者的区别
四者的区别(*和++优先级相同默认从右向左运算) ++*p相当于++(*p),表示先给p指向的变量值加1,然后取该变量的值. (*p)++相当于先取p指向的变量,然后该变量值加1. *p++相当于*( ...
- AppFuse 3的乱码问题
书接上回:AppFuse 3常见问题与解决方法,一个新问题:乱码! 在3.0.0版本号下,运行mvn appfuse:full-source命令后使用mvn jetty:run启动工程,页面上就出现了 ...