Codeforces #259 (Div. 2) B. Little Pony and Sort by Shift
2014-08-02 11:13
288 查看
很水的一道题啊,抽象出来就很简单了
其实就是给你一个环,让你判断这个环能否从某处开始按照非递减顺序排列
这样的话我们就可以这样做:
1、这个序列是一个非递减序列,输出0即可
2、这个序列存在两段非递减子序列,我们只需要比较第二段的最后一个位置和第一段的第一个位置相对大小即可
若小于等于,则可以满足条件,对应输出为第二段子序列的长度
3、这个序列存在两段以上非递减子序列,这样直接输出-1,应为无论怎样移动都不可能满足条件
以后不再把变量总结后输出,直接输出后return 0,这样不容易出错
代码如下:
其实就是给你一个环,让你判断这个环能否从某处开始按照非递减顺序排列
这样的话我们就可以这样做:
1、这个序列是一个非递减序列,输出0即可
2、这个序列存在两段非递减子序列,我们只需要比较第二段的最后一个位置和第一段的第一个位置相对大小即可
若小于等于,则可以满足条件,对应输出为第二段子序列的长度
3、这个序列存在两段以上非递减子序列,这样直接输出-1,应为无论怎样移动都不可能满足条件
以后不再把变量总结后输出,直接输出后return 0,这样不容易出错
代码如下:
#include <cstdio> #include <iostream> #include <algorithm> #define MAXN 100010 #define LL long long using 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 ) { ans = n-tmp+1; cout << ans << endl; return 0; } else { ans = -1; cout << -1 << endl; return 0; } } }
相关文章推荐
- 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 Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- Codeforces 259 B - Little Pony and Sort by Shift
- codeforces——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 KMP算法简单运用
- CF 454B(Little Pony and Sort by Shift-序列位移后单调性判断及最小位移[水])
- (周日赛)Little Pony and Sort by Shift
- B - Little Pony and Sort by Shift
- Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) A-Little Pony and Crystal Mine
- Codeforces Round #259 (Div. 1)——Little Pony and Harmony Chest
- Codeforces Round #259 (Div. 2) A. Little Pony and Crystal Mine
- Codeforces Div. 2 #259-A. Little Pony and Crystal Mine