Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. package GhostBusters;
  2.  
  3. import java.util.Collections;
  4. import java.util.LinkedList;
  5.  
  6. public class FindLine {
  7. //Ghost g has to be the leftmost point of all points in list1 & list2
  8. //This is used to solve the sub-problem
  9. //Find a pair of Ghostbuster and Ghost in O(n log n) time
  10. //such that the line generated by connecting these two
  11. //have the same number of Ghostbusters and Ghosts above the line
  12. //and the same number of Ghostbusters and Ghosts below the line
  13. //Same approach is applicable to Ghostbusters
  14. static int determinePair(Ghost g, LinkedList<GhostBuster> list1, LinkedList<Ghost> list2) {
  15. LinkedList<Pair> list1Pairs = new LinkedList<Pair>();
  16. LinkedList<Pair> list2Pairs = new LinkedList<Pair>();
  17. for(int i = 0; i < list1.size(); i++){
  18. list1Pairs.add(new Pair(i, list1.get(i).x - g.x, list1.get(i).y - g.y));
  19. list2Pairs.add(new Pair(i, list2.get(i).x - g.x, list2.get(i).y - g.y));
  20. }
  21. Collections.sort(list1Pairs);
  22. Collections.sort(list2Pairs);
  23. int low = 0;
  24. int high = list1.size() - 1;
  25. while(low <= high){
  26. int mid = (low + high)/2;
  27. int aboveGhostBusters = 0;
  28. int lowerGhostBusters = 0;
  29. int aboveGhosts = 0;
  30. int lowerGhosts = 0;
  31. double slope = list1Pairs.get(mid).slope;
  32. for(int i = 0; i < list1Pairs.size(); i++){
  33. if(i == mid) continue;
  34. if(list1Pairs.get(i).slope > slope){
  35. aboveGhostBusters++;
  36. }else{
  37. lowerGhostBusters++;
  38. }
  39. }
  40. for(int i = 0; i < list2Pairs.size(); i++){
  41. if(list2Pairs.get(i).index == list2.indexOf(g)) continue;
  42. if(list2Pairs.get(i).slope > slope){
  43. aboveGhosts++;
  44. }else{
  45. lowerGhosts++;
  46. }
  47. }
  48. if(aboveGhosts == aboveGhostBusters &&
  49. lowerGhosts == lowerGhostBusters){
  50. return list1Pairs.get(mid).index;
  51. }else if(aboveGhosts - aboveGhostBusters < 0){
  52. low = mid + 1;
  53. }else{
  54. high = mid - 1;
  55. }
  56. }
  57. return -1;
  58. }
  59. static class Pair implements Comparable<Pair>{
  60. int index;
  61. double slope;
  62. public Pair(int index, int xDist, int yDist){
  63. this.index = index;
  64. if(xDist == 0) this.slope = Double.MAX_VALUE;
  65. else this.slope = (((double)yDist)/(double)xDist);
  66. int coord = yDist + 1;
  67. int coord2 = xDist + 1;
  68. }
  69. @Override
  70. public int compareTo(Pair o) {
  71. return Double.compare(slope, o.slope);
  72. }
  73. }
  74. static class GhostBuster extends Coordinates{
  75. public GhostBuster(int x, int y) {
  76. super(x, y);
  77. }
  78. }
  79. static class Ghost extends Coordinates{
  80. public Ghost(int x, int y) {
  81. super(x, y);
  82. }
  83.  
  84. }
  85. static class Coordinates implements Comparable<Coordinates>{
  86. int x;
  87. int y;
  88. public Coordinates(int x, int y){
  89. this.x = x;
  90. this.y = y;
  91. }
  92. @Override
  93. public int compareTo(Coordinates o){
  94. return x == o.x ? Integer.compare(y, o.y) : Integer.compare(x, o.x);
  95. }
  96. }
  97. public static void main(String[] args){
  98. Ghost g = new Ghost(1,1);
  99. Ghost g2 = new Ghost(13,14);
  100. Ghost g3 = new Ghost(6,8);
  101. GhostBuster gb1 = new GhostBuster(13, -1);
  102. GhostBuster gb2 = new GhostBuster(4, 16);
  103. GhostBuster gb3 = new GhostBuster(12, -1);
  104. LinkedList<Ghost> list2 = new LinkedList<Ghost>();
  105. list2.add(g);
  106. list2.add(g2);
  107. list2.add(g3);
  108. LinkedList<GhostBuster> list1 = new LinkedList<GhostBuster>();
  109. list1.add(gb1);
  110. list1.add(gb2);
  111. list1.add(gb3);
  112. Collections.sort(list1);
  113. Collections.sort(list2);
  114. int index = determinePair(g, list1, list2);
  115. System.out.println(list1.get(index).x + " " + list1.get(index).y);
  116. System.out.println();
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement