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) 的值.求出互不相交的循环的个数,并输出每 ...
随机推荐
- msp430项目编程47
msp430综合项目---有线采集传输平台系统47 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- P2136 拉近距离(spfa判负环)
洛谷—— P2136 拉近距离 题目背景 我是源点,你是终点.我们之间有负权环. ——小明 题目描述 在小明和小红的生活中,有N个关键的节点.有M个事件,记为一个三元组(Si,Ti,Wi),表示从节点 ...
- c字符和字符数组/字符串
一维和二维的都可以:一维的情况如下:1,char string0[10];2,char string1[]="prison break";3,char string2[100]=& ...
- 四则运算表达式树 C++模板 支持括号和未知数
首先允许我吐槽CSDN的MARKDOWN,简直难用的不行. 程序的原理是将表达式分治转换为二叉树,再在二叉树上递归计算结果.如同以下表达式:x+5*y-(6/(1-5.5))可以表达为以下二叉树(抱歉 ...
- codeforces edu40
H(dp计数) 题意: 有一颗树,最深的点的深度是n,每个深度为i的点都有ai个孩子. 对于1<=k<=2n-2,回答树上有多少点对之间的距离是k,答案对1e9+7取模 n<=500 ...
- ,java反射机制实现拦截器
实现一个拦截器必须要实现一下几个类: 1 目标类接口:目标类要实现的接口. package com.lanvis.reflect; public interface ITarget { pub ...
- redis集群设置密码详解
原文:http://lookingdream.blog.51cto.com/5177800/1827851 注意事项: 1.如果是使用redis-trib.rb工具构建集群,集群构建完成前不要配置密码 ...
- LazyTableImages范例解析
近来想了解iPhone的多线程处理,查阅到了官方范例LazyTableImages,发现代码中有不仅有多线程处理,还有XML解析和异步下载图片,感觉官方例子真是全面啊,便花了时间好好研究下. Lazy ...
- Go -- 实现二叉搜索树
树: https://suanfa.herokuapp.com/3%E6%A0%91/binarytree/ 数据结构 首先我们定义需要的数据结构.注意,TreeNode的左右节点都是*TreeNod ...
- 【转载】云计算的三种服务模式:IaaS,PaaS和SaaS
一张图就看懂了.其他的都不用说了. 原文:http://blog.csdn.net/hjxgood/article/details/18363789