第一次月赛 Little Pony and Sort by Shift
来源:互联网 发布:linux apache 框架 编辑:程序博客网 时间:2024/06/10 20:00
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95560#problem/E
题意: 给出一个序列,可以进行的操作是: 一次只能将最尾端的元素放到最前面。如果可以将序列经过一系列的操作转化成一升序的序列,问需要挪动最尾端元素的次数,并输 出。如果不能排序,就输出-1;
样例: 输入4 1 2 输出 2
输入1 3 2 输出 -1
刚开始自己的思路想错了方向,导致一直wrong。看了一个比较简单的题解,解决这个问题的方法比较简单。
思路如下: 对于a1 , a2 ,a3 ~~~~~an,如果可以排序的话,那么必然存 在 : 1. 整个序列本身就是一个升序。2. 整个序列可以分为两段,每段都是升序排列的。如果是第二种情况,那么在第一段的结尾必然大于第二段的开头元素。 即:整个序列中当且仅当a[i] > a[i+1]这种情况只有一次。如果多于一次,必然输出-1.
则对整个序列进行查找,当a[i]<a[i+1]这种情况sum只出现一次的时候,还要进行判断,是否是在两段的接触点处发生,如果是,记录当时的位置。
#include <cstdio>#include <cstring>using namespace std;int a[100005];int main(){ int n,i; while(scanf("%d",&n)==1) { for(i=1;i<=n;++i) scanf("%d",&a[i]); int t=0; int flag = 1; for(i=2;i<=n;++i) { if(a[i]<a[i-1]) { t++; flag=i; } } if(t==0) printf("0\n"); else if(t>1) printf("-1\n"); else if(t==1 && a[n]<=a[1]) printf("%d\n",n-flag+1); else printf("-1\n"); } return 0;}
0 0
- 第一次月赛 Little Pony and Sort by Shift
- 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
- 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
- C--循环结构与结束语句
- docker本地graph存储以及 registry:v1 registry:v2(distribution)存储
- Oracle函数之LAG与LEAD
- const int *p与int *const p的区别
- Android SQLite数据库版本更新
- 第一次月赛 Little Pony and Sort by Shift
- 集合(关于collection)
- 封装Libsvm与Liblinear(重要概念之返回预测类别的概率分数)
- qtxlsx将excel中的数据读取到widget上
- 欢迎使用CSDN-markdown编辑器
- 创建Screen和MyRectangle类时遇到的问题总结
- 关于UFT的一些小想法
- Qt5窗口关闭与窗口销毁
- Swift2.0 对比 C++