fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <map>
  5. #include <deque>
  6. #include <cmath>
  7. #include <set>
  8. #include <unordered_map>
  9.  
  10. using namespace std;
  11.  
  12. #define all(v) ((v).begin()), ((v).end())
  13. #define sz(v) ((int)((v).size()))
  14. #define clr(v, d) memset(v, d, sizeof(v))
  15. #define rep(i, v) for (int i = 0; i < sz(v); ++i)
  16. #define lp(i, n) for (int i = 0; i < (int)(n); ++i)
  17. #define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
  18. #define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
  19. typedef long long ll;
  20.  
  21. void fast_io()
  22. {
  23. ios_base::sync_with_stdio(false);
  24. cin.tie(nullptr);
  25. cout.tie(nullptr);
  26. #ifndef ONLINE_JUDGE
  27. freopen("input.txt", "r", stdin);
  28. freopen("output.txt", "w", stdout);
  29. #endif
  30. }
  31. int main()
  32. {
  33. fast_io();
  34. int n;
  35. cin >> n;
  36. int arr[n];
  37. int maxIndex = 0;
  38. for (int i = 0; i < n; i++){
  39. cin >> arr[i];
  40. if (arr[maxIndex] <= arr[i])
  41. maxIndex = i;
  42. }
  43. int operations = 0;
  44. for (int i = maxIndex+1; i < n; i++){
  45. if (arr[n-i-1] < arr[i]){
  46. cout << -1 << endl;
  47. return 0;
  48. }else{
  49. operations++;
  50. }
  51. }
  52. cout <<operations << endl;
  53. }
Success #stdin #stdout 0.01s 5280KB
stdin
2
2 1
stdout
1