Codeforces #259 (Div. 2) B. Little Pony and Sort by Shift
来源:互联网 发布:学生购买阿里云服务器 编辑:程序博客网 时间:2024/05/17 05:08
很水的一道题啊,抽象出来就很简单了
其实就是给你一个环,让你判断这个环能否从某处开始按照非递减顺序排列
这样的话我们就可以这样做:
1、这个序列是一个非递减序列,输出0即可
2、这个序列存在两段非递减子序列,我们只需要比较第二段的最后一个位置和第一段的第一个位置相对大小即可
若小于等于,则可以满足条件,对应输出为第二段子序列的长度
3、这个序列存在两段以上非递减子序列,这样直接输出-1,应为无论怎样移动都不可能满足条件
以后不再把变量总结后输出,直接输出后return 0,这样不容易出错
代码如下:
#include <cstdio>#include <iostream>#include <algorithm>#define MAXN 100010#define LL long longusing namespace std;int n, a[MAXN];int main(void) { int ans = 0; scanf("%d", &n); for(int i=1; i<=n; ++i) scanf("%d", &a[i]); a[n+1] = -1; int tmp = -1; for(int i=1; i<=n; ++i) { if(a[i] > a[i+1]) { tmp = i+1; break; } } if(tmp > n) { ans = 0; cout << 0 << endl; return 0; } else { for(int i=tmp; i<n; ++i) { if(a[i] > a[i+1]) { ans = -1; cout << -1 << endl; return 0; } } if(a[1] >= a[n]) { ans = n-tmp+1; cout << ans << endl; return 0; } else { ans = -1; cout << -1 << endl; return 0; } } }
0 0
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2)-B. Little Pony and Sort by Shift
- Codeforces #259 (Div. 2) B. Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(模拟)
- Codeforces Div. 2 #259-B. Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- Codeforces Round #259 (Div. 2) B - Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift
- Codeforces 454B. Little Pony and Sort by Shift
- Codeforces 454B Little Pony and Sort by Shift(水题)
- CodeForces 454B Little Pony and Sort by Shift
- B. Little Pony and Sort by Shift
- B - Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shif
- CF 454 B. Little Pony and Sort by Shift
- CF 454B Little Pony and Sort by Shift
- Little Pony and Sort by Shift
- JAVA 实现二叉树 递归
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- acdream--Matrix sum
- 关于内存映射来修改文件,并改变文件的大小
- SQL Error: 17059, SQLState: 99999 错误原因
- Codeforces #259 (Div. 2) B. Little Pony and Sort by Shift
- jordan shoes discount options is beneficial
- [水]ZOJ1016
- hdu1201-18岁生日
- shell程序设计笔记
- 进制转换(2031)
- Codeforces Round #259 (Div. 2) A.Little Pony and Crystal Mine
- hduoj2802F(N)
- Weevely(php菜刀)工具使用简介