codeforce 259B 第二场 最新题解 (找规律)

男娘i 2022-08-10 15:52 240阅读 0赞

B. Little Pony and Sort by Shift

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

One day, Twilight Sparkle is interested in how to sort a sequence of integers a1, a2, …, a**n 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, …, a**n → a**n, a1, a2, …, a**n - 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, …, a**n (1 ≤ a**i ≤ 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

  1. 2
  2. 2 1

output

  1. 1

input

  1. 3
  2. 1 3 2

output

  1. -1

input

  1. 2
  2. 1 2

output

  1. 0
  2. /*codefoce 259B
  3. 这道题主要还是找规律吧,就是自己多模拟一些事例会发现这样的规律:
  4. 就是扫过去,开始严格递增,后来又递减,就是从由递增到递减的变化的位置+1开始数起,
  5. 如果以后全是递增把其个数累加起来会发现就是需要的最小移动的步数,
  6. 若后面不是严格递增的就输出-1;规律题就是这样,找到了代码实现很简单的
  7. 具体是实现见代码:
  8. 如有bug请各位大神指出,菜鸟不胜感激。
  9. */
  10. # include<iostream>
  11. # include<cstdio>
  12. # include<cstring>
  13. # include<cstdlib>
  14. # include<algorithm>
  15. # include<cmath>
  16. int s[100005];
  17. using namespace std;
  18. int n,t,ans,i;
  19. int main()
  20. {
  21. while(cin>>n)
  22. {
  23. ans=0;
  24. for(i=0;i<n;i++)
  25. {
  26. cin>>s[i];
  27. }
  28. for(i=0;i<n;i++)
  29. {
  30. if(s[i]>s[i+1])
  31. {
  32. t=i;
  33. break;
  34. }
  35. }
  36. for(i=t+1;i<n;i++)
  37. {
  38. if(s[i]<=s[(i+1)%n]) ans++;
  39. else break;
  40. }
  41. if(i==n) cout<<ans<<endl;
  42. if(i<n)cout<<-1<<endl;
  43. }
  44. return 0;
  45. }

发表评论

表情:
评论列表 (有 0 条评论,240人围观)

还没有评论,来说两句吧...

相关阅读