fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int frogs(int N, vector<int> arr)
  5. {
  6. if (N <= 1) return 0;
  7. if (arr[0] == 0) return -1;
  8.  
  9. int maxReach = arr[0];
  10. int step = arr[0];
  11. int jump = 1;
  12. int i = 1;
  13. for (i = 1; i < N+1; i++) {
  14. if (i == N) return jump;
  15. maxReach = max(maxReach, i + arr[i]);
  16. step--;
  17. if (step == 0) {
  18. jump++;
  19. if (i >= maxReach) return -1;
  20. step = maxReach - i;
  21. }
  22. }
  23.  
  24. return -1;
  25. }
  26. int main() {
  27. int n;
  28. cin>>n;
  29. vector<int> arr(n);
  30. for(int i=0;i<n;i++)
  31. cin>>arr[i];
  32.  
  33. int result = frogs(n,arr);
  34. cout<<result;
  35. return 0;
  36. }
Success #stdin #stdout 0s 5336KB
stdin
5
1
2
2
2
3
stdout
3