fork download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4.  
  5. bool FindPit(int* _Array, int _Size, int& inout_From, int& out_To, int& out_Max)
  6. {
  7. out_To = inout_From;
  8. out_Max = 0;
  9.  
  10. int lv_Max = _Array[inout_From];
  11. bool lv_LFind = true;
  12.  
  13. for(int i = inout_From; i < _Size; ++i)
  14. {
  15. if(lv_LFind)
  16. {
  17. if(_Array[i] >= lv_Max)
  18. {
  19. lv_Max = _Array[i];
  20. inout_From = i;
  21. }
  22. else
  23. lv_LFind = false;
  24. }
  25.  
  26. if(!lv_LFind)
  27. {
  28. if(_Array[i] >= out_Max)
  29. {
  30. out_Max = _Array[i];
  31. out_To = i;
  32. }
  33. if(out_Max >= lv_Max)
  34. {
  35. out_Max = lv_Max;
  36. return true;
  37. }
  38. }
  39. }
  40.  
  41. return out_To > inout_From;
  42. }
  43.  
  44. int Volume(const int* _Array,const int _From,const int _To,const int Max)
  45. {
  46. int lv_Volume = 0;
  47.  
  48. for(int i = _From; i < _To; i++)
  49. lv_Volume += (Max - _Array[i]) > 0 ? (Max - _Array[i]) : 0;
  50.  
  51. return lv_Volume;
  52. }
  53.  
  54. int Volume(int* _Array, int _Size)
  55. {
  56. int lv_Volume = 0;
  57. int lv_From = 0;
  58. int lv_To;
  59. int lv_Max;
  60.  
  61. while(FindPit(_Array, _Size, lv_From, lv_To, lv_Max))
  62. {
  63. auto lv_Vol = Volume(_Array, lv_From, lv_To, lv_Max);
  64. lv_Volume += lv_Vol;
  65. lv_From = lv_To;
  66. }
  67.  
  68. return lv_Volume;
  69. }
  70.  
  71. int main()
  72. {
  73. // 2,5,1,3,8,2,1,7,7,6 ans 17
  74. // 2,5,1,2,3,4,7,7,6 ans 10
  75. // 2,5,1,3,1,2,1,7,7,6 ans 17
  76. int lv_Array[] = {1, 2, 3, 2, 5, 3, 4};
  77. auto lv_Volume = Volume(lv_Array, sizeof(lv_Array)/sizeof(*lv_Array));
  78.  
  79. if( lv_Volume == 2)
  80. cout << "PASS";
  81. else
  82. cout << "FAIL";
  83.  
  84. return 0;
  85. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
PASS