fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int n;
  8. cin >> n;
  9. vector<int> a(n, 0);
  10. for (int i = 0; i < n; ++i)
  11. cin >> a[i];
  12.  
  13. int res = 0;
  14.  
  15. int has = 0;
  16. while (true)
  17. {
  18. for (int i = 0; i < n; ++i)
  19. if (a[i] <= has)
  20. {
  21. ++has;
  22. a[i] = 2000;
  23. }
  24.  
  25. if (has == n)
  26. break;
  27.  
  28. ++res;
  29.  
  30. for (int i = n - 1; i >= 0; --i)
  31. if (a[i] <= has)
  32. {
  33. ++has;
  34. a[i] = 2000;
  35. }
  36.  
  37. if (has == n)
  38. break;
  39.  
  40. ++res;
  41. }
  42.  
  43. cout << res;
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 3460KB
stdin
7
0 3 1 0 5 2 6
stdout
2