fork download
  1. #include <cstddef>
  2. #include <iostream>
  3.  
  4. template <std::size_t N>
  5. int calc_volume(int const (&heights)[N])
  6. {
  7. int const max_height = 9;
  8.  
  9. int volume = 0;
  10. int max_left_height = 0;
  11. int prev_height = max_height;
  12.  
  13. int tmp_volume[max_height + 1] = {};
  14.  
  15. for (int i = 0; i != N; ++i)
  16. {
  17. int const cur_height = heights[i];
  18.  
  19. if (cur_height > max_height)
  20. return -1;
  21.  
  22. for (int j = prev_height; j < cur_height; ++j)
  23. {
  24. volume += tmp_volume[j];
  25. tmp_volume[j] = 0;
  26. }
  27.  
  28. for (int j = cur_height; j < max_left_height; ++j)
  29. ++tmp_volume[j];
  30.  
  31. if (cur_height > max_left_height)
  32. max_left_height = cur_height;
  33.  
  34. prev_height = cur_height;
  35. }
  36.  
  37. return volume;
  38. }
  39.  
  40. int main()
  41. {
  42. int const heights1[] = { 2, 5, 1, 2, 3, 4, 7, 7, 6 }; // 10
  43. int const heights2[] = { 2, 5, 1, 3, 1, 2, 1, 7, 7, 6 }; // 17
  44. int const heights3[] = { 2, 5, 1, 3, 8, 2, 1, 7, 7, 6 }; // 17
  45. int const heights4[] = { 4, 3, 1, 5, 8, 0, 4, 0 ,0 , 5, 5, 7, 5, 8, 3, 3 }; // 42
  46. int const heights5[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; // 0
  47. int const heights6[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 }; // 0
  48. int const heights7[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9 }; // 45
  49. int const heights8[] = { 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1 }; // 1
  50. int const heights9[] = { 7, 1, 7, 2, 5 }; // 9
  51.  
  52. int const vol = calc_volume(heights1);
  53.  
  54. std::cout << "volume: " << vol;
  55.  
  56. return 0;
  57. }
  58.  
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
volume: 10