CF 454 B. Little Pony and Sort by Shift
2014-08-02 19:21
465 查看
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.
Sample test(s)
input
output
input
output
input
output
哎,无力吐槽,匿了。
策略:因为是升序,我们可以考虑首尾相连这样从最小值开始顺时针按升序排序符合就输出n-断点位子,否则输出-1;
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.
Sample test(s)
input
2 2 1
output
1
input
3 1 3 2
output
-1
input
2 1 2
output
0
哎,无力吐槽,匿了。
策略:因为是升序,我们可以考虑首尾相连这样从最小值开始顺时针按升序排序符合就输出n-断点位子,否则输出-1;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<limits.h> using namespace std; const int maxn=1e5+10; int a[maxn]; int main() { int n; while(~scanf("%d",&n)) { int flag=0,pos; for(int i=0;i<n;i++) { scanf("%d",&a[i]); if(i&&a[i]<a[i-1]) { flag++; pos=i; } } if(flag==0) cout<<0<<endl; else if(flag==1&&a[0]>=a[n-1]) cout<<n-pos<<endl; else cout<<-1<<endl; } return 0; }
相关文章推荐
- CF 454B Little Pony and Sort by Shift KMP算法简单运用
- CF 454B(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 Shift(序列)
- (周日赛)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 Round #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
- B. Little Pony and Sort by Shift
- Little Pony and Sort by Shift
- Codeforces 259 B - Little Pony and Sort by Shift
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shif
- CF 453B(Little Pony and Harmony Chest-数列最小加减1更改方案,满足任意2数互质-位运算dp+最坏情况分析+记忆化搜索)
- D-- Little Pony and Expected Maximum(CF--453A
- 【CF 453A】 A. Little Pony and Expected Maximum(期望、快速幂)
- CF-453/C-Little Pony and Summer Sun Celebration