Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. )
  6.  
  7. type Building struct {
  8. left int
  9. height int
  10. right int
  11. }
  12.  
  13. func (b Building) ToSkyline() Skyline {
  14. return Skyline{{b.left, b.height}, {b.right, 0}}
  15. }
  16.  
  17. type Line struct {
  18. x int
  19. h int
  20. }
  21.  
  22. type Skyline []Line
  23.  
  24. // aの位置で高さ変更するかどうか
  25. // a, b: 水平線の開始座標, a.x > b.x
  26. // h: 現在の水平線の高さ
  27. // 高さ変更する場合水平線の開始座標を返す
  28. // 変更しない場合はnilを返す
  29. func check(a Line, b Line, h int) *Line {
  30. if a.h > b.h || (a.h == b.h && a.h != h) {
  31. // =と後ろの条件でa, bが同じ高さの場合を考慮
  32. return &a
  33. } else if a.h < b.h && b.h != h {
  34. // 低い場合は引き上げる
  35. return &Line{a.x, b.h}
  36. }
  37.  
  38. return nil
  39. }
  40.  
  41. func merge(a Skyline, b Skyline) Skyline {
  42. result := Skyline{}
  43. h := 0
  44. i, j := 0, 0
  45.  
  46. // 重ならない左側の水平線を追加
  47. for i < len(a) && a[i].x < b[0].x {
  48. h = a[i].h
  49. result = append(result, a[i])
  50. i++
  51. }
  52.  
  53. for j < len(b) && b[j].x < a[0].x {
  54. h = b[j].h
  55. result = append(result, b[j])
  56. j++
  57. }
  58.  
  59. for i < len(a) && j < len(b) {
  60. if a[i].x < b[j].x {
  61. if res := check(a[i], b[j-1], h); res != nil {
  62. result = append(result, *res)
  63. h = res.h
  64. }
  65. i++
  66. } else {
  67. if res := check(b[j], a[i-1], h); res != nil {
  68. result = append(result, *res)
  69. h = res.h
  70. }
  71. j++
  72. }
  73. }
  74.  
  75. // 重ならない右側の水平線を追加 (残り)
  76. for j < len(b) {
  77. result = append(result, b[j])
  78. j++
  79. }
  80.  
  81. for i < len(a) {
  82. result = append(result, a[i])
  83. i++
  84. }
  85.  
  86. return result
  87. }
  88.  
  89. func resolve(bs []Building) Skyline {
  90. length := len(bs)
  91. if length == 1 {
  92. return bs[0].ToSkyline()
  93. }
  94.  
  95. return merge(resolve(bs[:length/2]), resolve(bs[length/2:]))
  96. }
  97.  
  98. func test_merge(s1 Skyline, s2 Skyline) {
  99. fmt.Printf("%v\n", merge(s1, s2))
  100. fmt.Printf("%v\n", merge(s2, s1))
  101. }
  102.  
  103. func test_resolve(bs []Building) {
  104. fmt.Printf("%v\n", resolve(bs))
  105. }
  106.  
  107. func main() {
  108. test_merge(Skyline{{1, 2}, {2, 0}}, Skyline{{3, 2}, {4, 0}})
  109. test_merge(Skyline{{1, 2}, {3, 0}}, Skyline{{2, 3}, {4, 0}})
  110. test_merge(Skyline{{1, 3}, {3, 0}}, Skyline{{2, 2}, {4, 0}})
  111. test_merge(Skyline{{1, 3}, {5, 0}}, Skyline{{2, 5}, {3, 3}, {4, 2}, {7, 0}})
  112. test_merge(Skyline{{2, 4}, {3, 3}, {5, 5}, {7, 2}, {9, 0}}, Skyline{{4, 2}, {5, 1}, {6, 6}, {8, 1}, {11, 0}})
  113. test_merge(Skyline{{1, 2}, {4, 0}, {8, 2}, {10, 0}}, Skyline{{5, 2}, {7, 0}})
  114.  
  115. test_resolve([]Building{{1, 2, 4}, {5, 2, 7}, {8, 2, 10}})
  116. test_resolve([]Building{{1, 11, 5}, {2, 6, 7}, {3, 13, 9}, {12, 7, 16}, {14, 3, 25}, {19, 18, 22}, {23, 13, 29}, {24, 4, 28}})
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement