Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift(模拟)
2014-08-02 11:35
330 查看
题目链接:Codeforces Round #259 (Div. 2) B. Little Pony and Sort by Shift
求给出的序列最少移动多少次成为非下降序列。移动方式:只能将最后一个元素移到第一个位置 即:a1, a2, ..., an → an, a1, a2, ..., an - 1.
从后前开始搜非下降的子序列,然后前面的子序列接在其后面,最后判断变化后的序列是否是非下降序列即可。
AC代码:
求给出的序列最少移动多少次成为非下降序列。移动方式:只能将最后一个元素移到第一个位置 即:a1, a2, ..., an → an, a1, a2, ..., an - 1.
从后前开始搜非下降的子序列,然后前面的子序列接在其后面,最后判断变化后的序列是否是非下降序列即可。
AC代码:
#include<stdio.h> #include<string.h> int main() { int n,j; int a[100010]; int i,b[100010]; while(scanf("%d",&n)!=EOF) { memset(b,0,sizeof b); for(i=1;i<=n;i++) scanf("%d",&a[i]); int x=-1,cnt=0; for(i=n;i>=1;i--) { if(a[i]<a[i-1]) { x=i; break; } } if(x==-1) { printf("0\n"); continue; } for(i=x,j=0;i<=n;i++,j++) b[j]=a[i]; for(i=1;i<x;i++,j++) b[j]=a[i]; int mark=0; for(i=1;i<n;i++) { if(b[i]<b[i-1]) { mark=1; break; } } if(mark==1) printf("%d\n",-1); else printf("%d\n",n-x+1); } return 0; }
相关文章推荐
- 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 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
- B - Little Pony and Sort by Shift
- codeforces——Little Pony and Sort by Shift
- Codeforces 259 B - Little Pony and Sort by Shift
- CF 454B Little Pony and Sort by Shift KMP算法简单运用
- Little Pony and Sort by Shift
- CF 454B(Little Pony and Sort by Shift-序列位移后单调性判断及最小位移[水])
- CF 454 B. Little Pony and Sort by Shift
- (周日赛)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
- Codeforces Round #259 (Div. 2) A. Little Pony and Crystal Mine(简单模拟实现题)
- Codeforces 453 B Little Pony and Harmony Chest(Round 259 div.1 B/div.2 D)
- Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest
- Codeforces Round #259 (Div. 2) C Little Pony and Expected Maximum