fork(5) download
  1. <?php
  2.  
  3. class Cup
  4. {
  5. const MAX_CAPACITY = 2000;
  6. private $filling;
  7. private $capacity;
  8.  
  9. public function __construct(int $capacity)
  10. {
  11. if ($capacity < 0 || $capacity > self::MAX_CAPACITY) {
  12. throw new InvalidArgumentException();
  13. }
  14. $this->filling = 0;
  15. $this->capacity = $capacity;
  16. }
  17.  
  18. public function filling()
  19. {
  20. return $this->filling;
  21. }
  22.  
  23. public function fill(int $value)
  24. {
  25. if ($value < 0 || $value + $this->filling > $this->capacity) {
  26. throw new InvalidArgumentException();
  27. }
  28. $this->filling += $value;
  29. }
  30.  
  31. public function release(int $value)
  32. {
  33. if ($value < 0 || $value > $this->filling) {
  34. throw new InvalidArgumentException();
  35. }
  36.  
  37. $this->filling -= $value;
  38. }
  39.  
  40. public function isEmpty()
  41. {
  42. return $this->filling === 0;
  43. }
  44.  
  45. public function quantityForMaximumFilling()
  46. {
  47. return $this->capacity - $this->filling;
  48. }
  49. }
  50.  
  51. class CupService
  52. {
  53. public function pourFromOneCupToAnother(Cup $firstCup, Cup $secondCup)
  54. {
  55. if ($firstCup->isEmpty()) {
  56. return false;
  57. }
  58.  
  59. if ($secondCup->quantityForMaximumFilling() === 0) {
  60. return false;
  61. }
  62.  
  63. $fillValue = $firstCup->filling() > $secondCup->quantityForMaximumFilling() ?
  64. $secondCup->quantityForMaximumFilling() :
  65. $firstCup->filling();
  66. $firstCup->release($fillValue);
  67. $secondCup->fill($fillValue);
  68.  
  69. return true;
  70. }
  71. }
  72.  
  73. class CupGame
  74. {
  75. /**
  76.   * @var Cup[]
  77.   */
  78. private $cups = [];
  79.  
  80. public function play(
  81. int $numberOfCups,
  82. int $cupCapacity,
  83. array $initialFilling,
  84. array $numberOfCupToResultCapacity
  85. ) {
  86. if (count($initialFilling) !== $numberOfCups) {
  87. throw new InvalidArgumentException();
  88. }
  89.  
  90. foreach ($numberOfCupToResultCapacity as $numberOfCupToCapacityElement) {
  91. if ($numberOfCupToCapacityElement > $numberOfCups) {
  92. throw new InvalidArgumentException();
  93. }
  94. }
  95. $cupService = new CupService();
  96. $this->initializationCups($cupCapacity, $initialFilling);
  97. do {
  98. $wasPourFromOneCupToAnother = false;
  99. foreach ($this->cups as $numberOfCup => $cup) {
  100. $numberOfNextCup = $numberOfCup + 1;
  101. if (isset($this->cups[$numberOfNextCup])) {
  102. if ($cupService->pourFromOneCupToAnother($cup, $this->cups[$numberOfNextCup])) {
  103. $wasPourFromOneCupToAnother = true;
  104. }
  105. }
  106. }
  107. } while ($wasPourFromOneCupToAnother);
  108.  
  109. $result = [];
  110. foreach ($numberOfCupToResultCapacity as $numberOfCupToCapacityElement) {
  111. $result[] = $this->cups[$numberOfCupToCapacityElement]->filling();
  112. }
  113.  
  114. return $result;
  115. }
  116.  
  117. private function initializationCups(int $cupCapacity, array $initialFilling)
  118. {
  119. foreach ($initialFilling as $index => $initialFillingElement) {
  120. $cup = new Cup($cupCapacity);
  121. $cup->fill($initialFillingElement);
  122. $this->cups[$index + 1] = $cup;
  123. }
  124. }
  125. }
  126.  
  127. $inputData = [];
  128. $f = fopen('php://stdin', 'r');
  129. $iterator = 0;
  130. while ($line = fgets($f)) {
  131.  
  132. if ($iterator < 2) {
  133. $inputData[] = explode(' ', trim($line));
  134. } else {
  135. $inputData[] = trim($line);
  136. }
  137.  
  138. $iterator++;
  139. }
  140. fclose($f);
  141. $game = new CupGame;
  142. $result = $game->play(
  143. $inputData[0][0],
  144. $inputData[0][1],
  145. $inputData[1],
  146. array_slice($inputData, -$inputData[2])
  147. );
  148.  
  149. foreach ($result as $resultElement)
  150. {
  151. echo $resultElement . "\r\n";
  152. }
Success #stdin #stdout 0.03s 26140KB
stdin
5 5
1 2 4 4 5
3
1
2
5
stdout
0
1
5