Advertisement
Cloude

Galactic

Mar 24th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.33 KB | None | 0 0
  1. import java.util.StringTokenizer;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.io.OutputStream;
  10. import java.util.Random;
  11.  
  12. class Kattio extends PrintWriter {
  13. public Kattio(InputStream i) {
  14. super(new BufferedOutputStream(System.out));
  15. r = new BufferedReader(new InputStreamReader(i));
  16. }
  17. public Kattio(InputStream i, OutputStream o) {
  18. super(new BufferedOutputStream(o));
  19. r = new BufferedReader(new InputStreamReader(i));
  20. }
  21.  
  22. public int getInt() {
  23. return Integer.parseInt(nextToken());
  24. }
  25.  
  26. public double getDouble() {
  27. return Double.parseDouble(nextToken());
  28. }
  29.  
  30. public long getLong() {
  31. return Long.parseLong(nextToken());
  32. }
  33.  
  34. public String getWord() {
  35. return nextToken();
  36. }
  37.  
  38.  
  39.  
  40. private BufferedReader r;
  41. private String line;
  42. private StringTokenizer st;
  43. private String token;
  44.  
  45. private String peekToken() {
  46. if (token == null)
  47. try {
  48. while (st == null || !st.hasMoreTokens()) {
  49. line = r.readLine();
  50. if (line == null) return null;
  51. st = new StringTokenizer(line);
  52. }
  53. token = st.nextToken();
  54. } catch (IOException e) { }
  55. return token;
  56. }
  57.  
  58. private String nextToken() {
  59. String ans = peekToken();
  60. token = null;
  61. return ans;
  62. }
  63. }
  64.  
  65. class Node {
  66. Node left, right, parent;
  67. int index, score, penalty, height, size;
  68.  
  69. Node(int ind, int a, int b) {
  70. this.score = a;
  71. this.penalty = b;
  72. this.index = ind;
  73. }
  74.  
  75. String info() {
  76. return Integer.toString(this.index) + ", " + Integer.toString(this.score) + ", " + Integer.toString(this.penalty);
  77. }
  78.  
  79. }
  80.  
  81. class BinaryTree {
  82. Node root;
  83. Node[] list;
  84.  
  85. BinaryTree(int n) {
  86. this.list = new Node[n + 1];
  87. for (int i = 1; i < n + 1; i++) {
  88. list[i] = new Node(i, 0, 0);
  89. }
  90. }
  91.  
  92. int max(int a, int b) {
  93. a = (a > b) ? a : b;
  94. return a;
  95. }
  96.  
  97. int height(Node team) {
  98. //System.out.println("height");
  99.  
  100. if (team == null) {return -1;}
  101. else {
  102. int a = (team.left == null) ? -1 : team.left.height;
  103. int b = (team.right == null) ? -1 : team.right.height;
  104. return max(a, b) + 1;
  105. }
  106. }
  107.  
  108. int size(Node team) {
  109. //System.out.println("size");
  110.  
  111. if (team == null) {return 0;}
  112. else {
  113. int a = (team.left == null) ? 0 : team.left.size;
  114. int b = (team.right == null) ? 0 : team.right.size;
  115. return 1 + a + b;
  116. }
  117. }
  118.  
  119. Boolean comparer(Node teama, Node teamb) {
  120. System.out.println("compare " + teama.info() + " and " + teamb.info());
  121.  
  122. if (teama.score == teamb.score && teama.penalty == teamb.penalty) {
  123. return teama.index < teamb.index;
  124. }
  125. return teama.score > teamb.score || (teama.score == teamb.score && teama.penalty < teamb.penalty);
  126. }
  127.  
  128. //t.right must be != null
  129. void rotateleft(Node t) {
  130. System.out.println("rl " + t.info());
  131.  
  132. Node w = t.right;
  133. System.out.println(w.info());
  134. w.parent = t.parent;
  135. t.parent = w;
  136. t.right = w.left;
  137. if (w.left != null) w.left.parent = t;
  138. w.left = t;
  139. if (w.parent != null) {
  140. if (w.parent.right != null) {
  141. if (w.parent.right == t) {w.parent.right = w;}
  142. }
  143. if (w.parent.left != null) {
  144. if (w.parent.left == t) {w.parent.left = w;}
  145. }
  146. }
  147. if (t == this.root) {this.root = w;}
  148. t.height = height(t);
  149. w.height = height(w);
  150. if (w.parent != null) w.parent.height = height(w.parent);
  151. }
  152.  
  153. //t.left must be != null
  154. void rotateright(Node t) {
  155. System.out.println("rr " + t.info());
  156.  
  157. Node w = t.left;
  158. w.parent = t.parent;
  159. t.parent = w;
  160. t.left = w.right;
  161. if (w.right != null) w.right.parent = t;
  162. w.right = t;
  163. if (w.parent != null) {
  164. if (w.parent.right != null) {
  165. if (w.parent.right == t) {w.parent.right = w;}
  166. }
  167. if (w.parent.left != null) {
  168. if (w.parent.left == t) {w.parent.left = w;}
  169. }
  170. }
  171. if (t == this.root) {this.root = w;}
  172. t.height = height(t);
  173. w.height = height(w);
  174. if (w.parent != null) w.parent.height = height(w.parent);
  175. }
  176.  
  177. int bf(Node t) {
  178. int a = (t.left == null) ? -1 : t.left.height;
  179. int b = (t.right == null) ? -1 : t.right.height;
  180. return a - b;
  181. }
  182.  
  183. void insert(Node team) {
  184. System.out.println("insert " + team.info());
  185. team.parent = null;
  186. team.left = null;
  187. team.right = null;
  188. team.height = 0;
  189. team.size = 1;
  190. if (this.root == null) {
  191. this.root = team;
  192. }
  193. else {
  194. //traverses down where the node should go
  195. Node curr = this.root, prev = curr;
  196. while (curr != null) {
  197. prev = curr;
  198. curr = (comparer(curr, team)) ? curr.left : curr.right;
  199. }
  200. team.parent = prev;
  201. if (comparer(prev, team)) {prev.left = team;}
  202. else {prev.right = team;}
  203.  
  204. //updates the lineage for height
  205. fixavl(prev);
  206. }
  207. }
  208.  
  209. void fixavl(Node prev) {
  210. System.out.println("fixavl at " + prev.info());
  211. while (prev != null) {
  212. prev.height = height(prev);
  213. if (bf(prev) == 2) {
  214. System.out.println(bf(prev) + " is BF of " + prev.info());
  215.  
  216. if (bf(prev.left) == -1) {rotateleft(prev.left);}
  217. rotateright(prev);
  218. prev = prev.parent;
  219.  
  220. }
  221. else if (bf(prev) == -2) {
  222. if (bf(prev.right) == 1) {rotateright(prev.right);}
  223. rotateleft(prev);
  224. prev = prev.parent;
  225. }
  226. prev = prev.parent;
  227. }
  228. }
  229.  
  230. Node lowest(Node from) {
  231. System.out.println("lowest");
  232.  
  233. Node low = from;
  234. if (low == null) {return null;}
  235. else {
  236. while (low.left != null) {low = low.left; System.out.println("here?");}
  237. return low;
  238. }
  239. }
  240.  
  241. void remove(Node team) {
  242. System.out.println("removing " + team.info());
  243.  
  244. if (team.left == null && team.right == null) {
  245. if (this.root == team) {this.root = null;}
  246. else {
  247. if (team.parent.right == team) {team.parent.right = null;}
  248. else {team.parent.left = null;}
  249. fixavl(team.parent);
  250. }
  251. }
  252.  
  253. else if (team.left != null && team.right != null) {
  254. System.out.println("THIS CURSED PART");
  255. Node child = team.right;
  256. while (child.left != null) {child = child.left;}
  257. this.list[team.index] = child;
  258. this.list[child.index] = team;
  259. int tindex = team.index, tscore = team.score, tpenalty = team.penalty;
  260. team.index = child.index;
  261. team.score = child.score;
  262. team.penalty = child.penalty;
  263. child.index = tindex;
  264. child.score = tscore;
  265. child.penalty = tpenalty;
  266.  
  267. remove(child);
  268. }
  269.  
  270. else {
  271. if (team == this.root) {
  272. if (team.left != null) {
  273. this.root = team.left;
  274. team.left.parent = null;
  275. }
  276. else {
  277. this.root = team.right;
  278. team.right.parent = null;
  279. }
  280. fixavl(this.root);
  281. }
  282. else {
  283. System.out.println("here");
  284. Node temp = (team.left != null) ? team.left : team.right;
  285. if (team.parent.right != null) {
  286. if (team.parent.right.index == team.index) {
  287. team.parent.right = temp;
  288. temp.parent = team.parent;
  289. }
  290. }
  291. if (team.parent.left != null) {
  292. if (team.parent.left.index == team.index) {
  293. team.parent.left = temp;
  294. temp.parent = team.parent;
  295. }
  296. }
  297. fixavl(team.parent);
  298. }
  299. }
  300. }
  301.  
  302. Node successor(Node curr) {
  303. Node temp = curr;
  304. if (curr.right != null) {return lowest(curr.right);}
  305. while (temp.parent != null) {
  306. if (temp.parent.left != null) {
  307. if (temp.parent.left.index == temp.index) {return temp.parent;}
  308. }
  309. temp = temp.parent;
  310. }
  311. return null;
  312. }
  313.  
  314. }
  315. public class Galactic {
  316.  
  317. public static void main(String[] args) {
  318. Kattio br = new Kattio(System.in);
  319. Random rng = new Random();
  320. int nn = br.getInt(), n = br.getInt();
  321. BinaryTree tree = new BinaryTree(nn);
  322. boolean[] intree = new boolean[nn+1];
  323. int rank = 1;
  324.  
  325. for (int i = 0; i < n; i++) {
  326. int newteam = rng.nextInt(nn) + 1, newpenal = rng.nextInt(3);
  327. System.out.println(newteam + " " + newpenal);
  328. System.out.println(tree.list[newteam].info());
  329. //int newteam = br.getInt(), newpenal = br.getInt();
  330. if (intree[newteam]) {tree.remove(tree.list[newteam]);}
  331. tree.list[newteam].score++;
  332. tree.list[newteam].penalty += newpenal;
  333.  
  334. if (newteam == 1) {
  335. Node low = tree.lowest(tree.root);
  336. while(low != null && tree.comparer(tree.list[1], low)) {
  337. rank--;
  338. Node temp = tree.successor(low);
  339. tree.remove(low);
  340. intree[low.index] = false;
  341. low = temp;
  342. }
  343. }
  344.  
  345. else if (newteam != 1 && tree.comparer(tree.list[newteam], tree.list[1])) {
  346. if (!intree[newteam]) {rank++;}
  347. intree[newteam] = true;
  348. tree.insert(tree.list[newteam]);
  349. }
  350. System.out.println("\n NODE LIST");
  351.  
  352. for (Node node : tree.list) {
  353. if (node != null) {
  354. if (intree[node.index]) {
  355. if (node.left != null) System.out.print(node.left.info());
  356. else {System.out.print("null");}
  357. System.out.print(" " + node.info() + " ");
  358. if (node.right != null) System.out.print(node.right.info());
  359. else {System.out.print("null");}
  360. System.out.print("- height:" + node.height);
  361. System.out.print("\n");
  362.  
  363.  
  364. }
  365. }}
  366.  
  367. br.println(rank);
  368.  
  369. }
  370.  
  371. br.close();
  372.  
  373. }
  374.  
  375. }
  376. /*
  377. 3 4
  378. 2 7
  379. 3 5
  380. 1 6
  381. 1 9
  382.  
  383. CREATES BUG
  384. 8 2
  385. 5 1
  386. 10 2
  387. 4 2
  388. 7 1
  389.  
  390. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement