StormFalcon32

PieForPie

Dec 31st, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Arrays;
  8. import java.util.LinkedList;
  9. import java.util.StringTokenizer;
  10.  
  11. public class PieForPie {
  12.  
  13. int N;
  14. int D;
  15. Pie[] bessie = new Pie[N];
  16. Pie[] elsie = new Pie[N];
  17. int[] t;
  18. int[] d;
  19. boolean[] visitedBessie;
  20. boolean[] visitedElsie;
  21.  
  22. public PieForPie(int N, int D, Pie[] bessie, Pie[] elsie) {
  23. this.N = N;
  24. this.D = D;
  25. this.bessie = bessie;
  26. this.elsie = elsie;
  27. t = new int[N];
  28. d = new int[N];
  29. visitedBessie = new boolean[N];
  30. visitedElsie = new boolean[N];
  31. }
  32.  
  33. public int[] bfs() {
  34. LinkedList<Pie> q = new LinkedList<Pie>();
  35. for (int i = 0; i < N; i++) {
  36. Pie curr = bessie[i];
  37. if (curr.otherScore == 0) {
  38. q.add(curr);
  39. t[curr.num] = 1;
  40. visitedBessie[curr.num] = true;
  41. }
  42. }
  43. for (int i = 0; i < N; i++) {
  44. Pie curr = elsie[i];
  45. if (curr.otherScore == 0) {
  46. q.add(curr);
  47. d[curr.num] = 1;
  48. visitedElsie[curr.num] = true;
  49. }
  50. }
  51.  
  52. while (!q.isEmpty()) {
  53. Pie curr = q.poll();
  54. if (curr.bessie) {
  55. int found = lowerBound(elsie, curr.score);
  56. while (found >= 0 && elsie[found].otherScore >= curr.score - D) {
  57. Pie other = elsie[found];
  58. if (!visitedElsie[other.num]) {
  59. visitedElsie[other.num] = true;
  60. d[other.num] = t[curr.num] + 1;
  61. q.add(other);
  62. }
  63. found--;
  64. }
  65. } else {
  66. int found = lowerBound(bessie, curr.score);
  67. while (found >= 0 && bessie[found].otherScore >= curr.score - D) {
  68. Pie other = bessie[found];
  69. if (!visitedBessie[other.num]) {
  70. visitedBessie[other.num] = true;
  71. t[other.num] = d[curr.num] + 1;
  72. q.add(other);
  73. }
  74. found--;
  75. }
  76. }
  77. }
  78. return t;
  79. }
  80.  
  81. public int lowerBound(Pie[] pies, int find) {
  82. int min = 0;
  83. int max = N;
  84. int mid = -1;
  85. boolean found = false;
  86.  
  87. while (min < max) {
  88. mid = (min + max) / 2;
  89. Pie toTry = pies[mid];
  90. if (toTry.otherScore == find) {
  91. found = true;
  92. break;
  93. } else if (toTry.otherScore < find) {
  94. min = mid + 1;
  95. } else {
  96. max = mid;
  97. }
  98. }
  99. if (found) {
  100. // Check for duplicates
  101. int i;
  102. for (i = mid; i < max; i++) {
  103. if (pies[i].otherScore > find) {
  104. break;
  105. }
  106. }
  107. return i - 1;
  108. }
  109. return Math.min(N - 1, min - 1);
  110. }
  111.  
  112. public static void main(String[] args) throws IOException {
  113. // BufferedReader in = new BufferedReader(new
  114. // FileReader("C:\\Users\\bench\\git\\USACO-Gold\\Gold\\PieForPie\\2.in"));
  115. // BufferedReader in = new BufferedReader(new
  116. // FileReader("H:\\git\\USACO-Gold\\Gold\\PieForPie\\1.in"));
  117. BufferedReader in = new BufferedReader(new FileReader("piepie.in"));
  118. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("piepie.out")));
  119. StringTokenizer tk = new StringTokenizer(in.readLine());
  120. int N = Integer.parseInt(tk.nextToken());
  121. int D = Integer.parseInt(tk.nextToken());
  122. Pie[] bessie = new Pie[N];
  123. Pie[] elsie = new Pie[N];
  124. for (int i = 0; i < N; i++) {
  125. tk = new StringTokenizer(in.readLine());
  126. int score = Integer.parseInt(tk.nextToken());
  127. int otherScore = Integer.parseInt(tk.nextToken());
  128. bessie[i] = new Pie(score, otherScore, i, true);
  129. }
  130. for (int i = 0; i < N; i++) {
  131. tk = new StringTokenizer(in.readLine());
  132. int score = Integer.parseInt(tk.nextToken());
  133. int otherScore = Integer.parseInt(tk.nextToken());
  134. elsie[i] = new Pie(otherScore, score, i, false);
  135. }
  136. Arrays.sort(bessie);
  137. Arrays.sort(elsie);
  138. PieForPie solver = new PieForPie(N, D, bessie, elsie);
  139. int[] dists = solver.bfs();
  140. for (int i = 0; i < N; i++) {
  141. out.println(dists[i] == 0 ? -1 : dists[i]);
  142. }
  143. out.close();
  144. in.close();
  145. }
  146. }
  147.  
  148. class Pie implements Comparable<Pie> {
  149. int score;
  150. int otherScore;
  151. int num;
  152. boolean bessie;
  153.  
  154. public Pie(int score, int otherScore, int num, boolean bessie) {
  155. this.score = score;
  156. this.otherScore = otherScore;
  157. this.num = num;
  158. this.bessie = bessie;
  159. }
  160.  
  161. @Override
  162. public int compareTo(Pie other) {
  163. return Integer.compare(this.otherScore, other.otherScore);
  164. }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment