Little Pony and Sort by Shift
来源:互联网 发布:angularjs js cdn 编辑:程序博客网 时间:2024/05/17 01:45
One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, ..., an in non-decreasing order. Being a young unicorn, the only operation she can perform is a unit shift. That is, she can move the last element of the sequence to its beginning:
a1, a2, ..., an → an, a1, a2, ..., an - 1.
Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
Input
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
Output
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
题目大意是说一个序列只能把底部元素放到首部。目标是把该序列调整成不下降序列。经过题目给的样例和手测数据基本可以确定一个可调整的序列满足A[1]~A[i]不下降,A[i+1]~A[n]不下降且A[n]<=A[1(这一点需要自己去体会为什么)。于是根据这个策略可以写出如下程序(程序写的有一些复杂,将就下吧,毕竟LZ才从pascal转C++....)
#include<iostream>using namespace std;long a[100002];int main(){ long n=0; cin >> n; for (long i=0;i<=n-1;++i) cin >> a[i]; long i=0,j=0; while (a[i]<=a[i+1]) ++i; if (i==n-1) {cout <<0; return 0;} ++i; ++j; while (a[i]<=a[i+1]) {++i; ++j;} if (i!=n-1) { cout <<-1; return 0; } else if (a[n-1]>a[0]) {cout <<-1; return 0;} else cout <<j; return 0; }
- B. Little Pony and Sort by Shift
- Little Pony and Sort by Shift
- B - Little Pony and Sort by Shift
- Codeforces 454B. Little Pony and Sort by Shift
- CF 454 B. Little Pony and Sort by Shift
- Codeforces 454B Little Pony and Sort by Shift(水题)
- CF 454B Little Pony and Sort by Shift
- 第一次月赛 Little Pony and Sort by Shift
- CodeForces 454B 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 (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
- jdk环境变量配置
- hdoj 4906 状态dp
- Android实现无线调试自己的应用
- 基于单片机的语音终端设计
- windows已在 XXX.exe 中触发断点
- Little Pony and Sort by Shift
- Yaroslav and Sequence - CodeForces 301A 水题
- 软件测试与质量控制
- 简介→_→
- burpsuite学习
- 探究计算机C语言编辑程序技巧
- Gdiplus的坑:通过DrawImage进行放大不是很靠谱
- Little Pony and Expected Maximum
- FFmpeg MP3解码器的研究与优化