Guest User

Untitled

a guest
Jun 22nd, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. <?php
  2.  
  3. function solution(int $x, int $y, int $k, array $a, array $b): int
  4. {
  5. $widths = length_calc($x, $a);
  6. $heights = length_calc($y, $b);
  7.  
  8. return size($widths, $heights, $k);
  9. }
  10.  
  11. function length_calc(int $length, array $sections): array
  12. {
  13. $data = [];
  14.  
  15. $count_sections = \count($sections);
  16.  
  17. foreach ($sections as $section_id => $section) {
  18. $data[] = $section - ($sections[$section_id - 1] ?? 0);
  19.  
  20. if ($count_sections === $section_id + 1) {
  21. $data[] = $length - ($sections[$section_id - 1] ?? 0);
  22. }
  23. }
  24.  
  25. sort($data, SORT_NUMERIC);
  26.  
  27. return $data;
  28. }
  29.  
  30. function size(array $w, array $h, int $o): int
  31. {
  32. $N = \count($w);
  33.  
  34. $start = 1;
  35. $end = $w[$N - 1] * $h[$N - 1];
  36.  
  37. while ($start <= $end) {
  38. $middle = ($start + $end) >> 1;
  39.  
  40. $cal = cal($middle, $w, $h);
  41.  
  42. if ($cal >= $o) {
  43. $start = $middle + 1;
  44. } else {
  45. $end = $middle - 1;
  46. }
  47. }
  48.  
  49. return $start;
  50. }
  51.  
  52. function cal(int $size, array $w, array $h): int
  53. {
  54. $N = \count($w);
  55.  
  56. if ($w[$N - 1] * $h[$N - 1] <= $size) {
  57. return 0;
  58. }
  59.  
  60. $result = 0;
  61. $hIndex = $N - 1;
  62.  
  63. foreach (range(0, $N) as $wIndex) {
  64. while ($hIndex >= 0 && $w[$wIndex] * $h[$hIndex] > $size) {
  65. $hIndex -= 1;
  66. }
  67.  
  68. $result += $N - 1 - $hIndex;
  69. }
  70.  
  71. return $result;
  72. }
  73.  
  74. $x = 35;
  75. $y = 38;
  76. $k = 15;
  77.  
  78. $a = [1, 3, 6, 11, 24, 26, 32];
  79. $b = [1, 5, 8, 13, 16, 22, 26];
  80.  
  81. echo solution($x, $y, $k, $a, $b);
Add Comment
Please, Sign In to add comment