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) 的值.求出互不相交的循环的个数,并输出每 ...
随机推荐
- Python入门---易错已错易混淆----知识点
1.not 1 or 0 and 1 or 3 and 4 or 5 and 6 or 7 and 8 and 9 结果会输出啥? 根据优先级:(not 1) or (0 and 1) or (3 a ...
- Android借助Handler,实现ViewPager中页面的自动切换(转)
在很多电商网页及app上都有自动切换的商品的推广快,感觉体验挺不错的,正好今天学习使用ViewPager,因此也实现了一个功能类似的demo. 下面是其中的两个截图: 实现一个自动 ...
- P1717 钓鱼 洛谷
https://www.luogu.org/problem/show?pid=1717 题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请 ...
- ros使用罗技f710无线控制手柄
参考:blog.csdn.net/hcx25909/article/details/9042469 罗技F710无线控制手柄ROS下使用说明 安装手柄相关的包和驱动 sudo apt-get inst ...
- luogu P1342 请柬
题目描述 在电视时代,没有多少人观看戏剧表演.Malidinesia古董喜剧演员意识到这一事实,他们想宣传剧院,尤其是古色古香的喜剧片.他们已经打印请帖和所有必要的信息和计划.许多学生被雇来分发这些请 ...
- HttpServletRequest接口是怎么实现的
request只是规范中的一个名称而已.不是SUN提供的,这是由各个不同的Servlet提供商编写的,SUN只是规定这个类要实现HttpServletRequest接口,并且规定了各个方法的用途,但具 ...
- ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客
原文:ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客 上一篇文章中我们已经成功的记录了Request部分的信息,现在我们来看下如何记录Res ...
- People seldom do what they believe in. They do what is convenient, then repent.
People seldom do what they believe in. They do what is convenient, then repent. 人们很少真正实践他们的理想.他们只做比较 ...
- 使用Python控制1602液晶屏实时显示时间(附PyCharm远程调试)
前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 本文介绍一下UP板的GPIO资源使用,以及一个使用Python演示一个简单的demo. 本文使用Markdown写成,为获得更好的 ...
- ng-options bug解决方案(示例)
情况: 无法获取 ng-model 的值 解决方案: 绑定到对象的属性值上 1.页面 <ion-view hide-nav-bar="true"> <ion-co ...