Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
来源:互联网 发布:专用软件 编辑:程序博客网 时间:2024/06/12 23:17
题目链接:http://codeforces.com/contest/454/problem/B
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:
Help Twilight Sparkle to calculate: what is the minimum number of operations that she needs to sort the sequence?
The first line contains an integer n (2 ≤ n ≤ 105). The second line contains n integer numbers a1, a2, ..., an (1 ≤ ai ≤ 105).
If it's impossible to sort the sequence output -1. Otherwise output the minimum number of operations Twilight Sparkle needs to sort it.
22 1
1
31 3 2
-1
21 2
0
思路:
判断有存在几个“下坡”,也就是后面的数比前面的小;
如果没有则输出0;如果有不止一个,则输出-1;如果恰有一个, 那么就需要判断a[1]和a[n]的大小关系;
代码如下:
#include <cstdio>#include <cstring>int main(){int n;int i, j;int a[100017];while(~scanf("%d",&n)){for(i = 1; i <= n; i++){scanf("%d",&a[i]);}int cont = 0;int t;for(i = 2; i <= n; i++){if(a[i] < a[i-1]){cont++;t = i;}}if(cont == 0)printf("0\n");else if(cont > 1)printf("-1\n");else if(cont == 1 && a[n] <= a[1])printf("%d\n",n-t+1);elseprintf("-1\n");}return 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 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 (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 Shif
- 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
- CF 454 B. Little Pony and Sort by Shift
- CF 454B Little Pony and Sort by Shift
- Codeforces 453 B Little Pony and Harmony Chest(Round 259 div.1 B/div.2 D)
- 关于HTML5中Canvas的宽、高设置有关问题
- matlab中关闭特定的warning
- matlab 中 train和adapt的区别
- matlab cell无法扩展的问题求解:
- Javascript 中的长字符串拼接
- Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(序列)
- cocos2d-x 3.x 关于cocostudio的UI将事件吃掉,场景Layer无法响应手势的问题解决方法
- poj1065
- matlab调用c dll
- ACM 数论 UVALive 6170 Esspe-Peasee 解二元一次方程 扩张欧几里得算法
- IOS-DEMO5 PLLCircleProgressView
- 网络流初步总结
- hdu 1207 汉诺塔II
- Github Pages 搭建个人博客